我已经获得了一个 CDI bean,它是使用以下代码以编程方式获得的:
MyBean bean = CDI.current().select(MyBean.class, qualifier).get();
Run Code Online (Sandbox Code Playgroud)
完成后,我是否需要使用销毁这个bean
CDI.current().destroy (bean);
Run Code Online (Sandbox Code Playgroud)
或者 bean 是否继承了我的类的范围?
在我的组织中,C开发人员开发了一个应用程序并作为.exe格式提供给我们.
如果任何最终用户发送任何Http请求.exe.这将运行,为此我写了Java代码(ProcessBuilder())并部署在Application Server(Apache Tomcat).It工作正常.这一切都在我的本地系统(Windows 7)完成.
但是我们正在使用它Ubuntu 12.04作为服务器.这个.exe文件不起作用.
我怎样才能解决这个问题.
我是Java新手,我开发了一个与普通Java客户端一起使用的示例Java EE应用程序,但它不适用于Servlet.我在Netbeans IDE中使用Application Client,EJB模块和Web模块开发了EE项目.
Customer.java
@Entity
@Table(name = "customer")
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column (name="id")
private String id;
@Column (name="name")
private String name;
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
Run Code Online (Sandbox Code Playgroud)
myBeanInterface.java
import Entities.Customer;
import javax.ejb.Remote;
@Remote
public interface myBeanInterface {
void addCustomer(Customer customer);
}
Run Code Online (Sandbox Code Playgroud)
CustomerSession.java
@Stateless
public class CustomerSession implements myBeanInterface { …Run Code Online (Sandbox Code Playgroud) 我正在尝试设置备用docroot以便从中提供上传的文档.我在glassfish web xml中包含以下内容
<context-root>/dom</context-root>
<property description="Uploaded Images" name="alternatedocroot_1" value="from=/uploads/* dir=C:/Test" />
Run Code Online (Sandbox Code Playgroud)
然后我将测试pdf存储在名为cars.pdf的Test文件夹中.
要访问它,我在浏览器中键入以下内容
http://localhost:8080/uploads/cars.pdf
Run Code Online (Sandbox Code Playgroud)
然而,这只是给我一个404错误,我已经尝试谷歌搜索和搜索这里但似乎没有任何工作.有人可以告诉我,我做错了什么?
谢谢Steve
问题:在Java EE和EJB的上下文中,任何人都可以显示具有两种不同方法的特定DAO类(或多个).和一个服务类在一个事务边界中调用这两个方法并回滚?
我有一个EJB,但我想将它用作服务层,就像在Spring @Transactional方法中一样.
1)这是个好主意吗?
2)如何在一个方法中的一个"事务"中进行多个dao方法调用?我想我必须在transaction.begin()和.上制定一些策略.COMIT()?任何人都可以展示代码示例吗?
一些主要优点是:
a-所有小的不可变DAO事务将在单个数据库连接中的"一次性"中提交(in single transactional boundries)
b-如果说我在服务器上有4个dao调用,而第三个调用失败,那么由于它的一个事务边界,我可以这样做roll backs.
c-我immutable DAO methods会re-usable在很多其他地方.
Java EE示例:
import com.....entities.Users;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
@Stateless
public class UsersBean {
// Add business logic below. (Right-click in editor and choose
// "Insert Code > Add Business Method")
public Users sayHello() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("CommunityPU");
EntityManager em = emf.createEntityManager();
Users user = em.find(Users.class, 1);
em.close();
emf.close();
return user;
}
} …Run Code Online (Sandbox Code Playgroud) 我们目前使用的是 Java EE 5,我们在发送响应之前执行以下操作将 POJO 转换为 JSON。
@GET
@Path("/books")
@Produces(MediaType.APPLICATION_JSON)
public Response getBooks() {
List<Book> listOfBooks = getMiscService().getbooks();
String response = "{\"books\":" + gson.toJson(listOfBooks) + "}";
return Response.status(Response.Status.OK).entity(response).build();
}
Run Code Online (Sandbox Code Playgroud)
我们正在使用谷歌的 gson API。现在我们正在将代码重组为符合 Java EE 7 API 的,我想知道是否有任何 JSON 转换 API 可以将 POJO 转换为 JSON。
我知道 Java EE 7 中引入的 JsonObject API。但我仍然想知道如何获得我的 POJO 的 JSON 表示。
JsonObject jsonObject = Json.createObjectBuilder().add("books", myObject);
Run Code Online (Sandbox Code Playgroud)
上面的 myObject 需要是我的对象的 JSON 表示正确吗?
我正在考虑这个。但这仍然使用 Gson
JsonObject jsonObject = Json.createObjectBuilder().add("books", gson.toJson(myObject));
Run Code Online (Sandbox Code Playgroud)
这里推荐的方式是什么?
谢谢
我正在本地 Glassfish 4.1.1 (WELD 2.2.2) 安装中运行一些关于 CDI 的概念验证。
我创建了一个名为 TipicalBean 的类 bean,它注入一个在生产者方法中生成的整数。
定义生产者方法的类如下所示
public class ProducerTest {
@Produces @MyNumber @Dependent public static int getMyNumber() {
return 100;
}
}
Run Code Online (Sandbox Code Playgroud)
这是行不通的。即使我将方法声明为静态
但如果将 @Dependent 作用域放入类定义中,它确实有效,如下所示:
@Dependent
public class ProducerTest {
Run Code Online (Sandbox Code Playgroud)
此外,如果我在注入它的同一个类中声明该生产者方法,我可以在生产者方法声明中定义 @Dependent 范围,但 WELD 会在 Glassfish 控制台中打印警告
WELD-000018: Executing producer field or method [BackedAnnotatedMethod] @Produces
@MyNumber @Dependent public mypackage.TipicalBean.produceMyNumber() on incomplete
declaring bean Managed Bean [class mypackage.TipicalBean] with qualifiers [@Any
@Default] due to circular injection
Run Code Online (Sandbox Code Playgroud)
为什么?生产者方法必须在 ManagedBeans 中定义吗?
由于我使用的是 JavaEE 7,因此我尚未创建 …
我试图在我通过Docker安装的Websphere Liberty配置文件中运行的非常简单的Web应用程序中使用CDI.
但是注入失败,除非我@ApplicationScoped在注入的bean上指定范围注释(例如),尽管根据许多在线教程(例如,这个),Java EE规范不要求这样做.
以下是失败的代码:
HelloWorldServlet.java
package my.simple.app;
import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/HelloWorld")
public class HelloWorldServlet extends HttpServlet {
static String PAGE_HEADER = "<html><head /><body>";
static String PAGE_FOOTER = "</body></html>";
@Inject
HelloService helloService;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter writer = resp.getWriter();
writer.println(PAGE_HEADER);
writer.println("<h1>" + helloService.createHelloMessage("World") + "</h1>");
writer.println(PAGE_FOOTER);
writer.close();
}
}
Run Code Online (Sandbox Code Playgroud)
HelloService.java
package my.simple.app;
public class HelloService { …Run Code Online (Sandbox Code Playgroud)
我正在阅读Java EE 7教程.在第13.12章中,有一个示例应用程序,ajaxguessnumber.我在Glassfish 4中运行示例,一切正常.然后我将System.out.println放在bean构造函数中,我意识到构造函数在初始页面加载期间被调用了两次.为什么会这样,即使是@SessionScoped bean?
这是xhtml文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<h:outputStylesheet library="css" name="default.css"/>
<title>Ajax Guess Number Facelets Application</title>
</h:head>
<h:body>
<h:form id="AjaxGuess">
<h:graphicImage value="#{resource['images:wave.med.gif']}"
alt="Duke waving his hand"/>
<h2>
Hi, my name is Duke. I am thinking of a number from
#{dukesNumberBean.minimum} to #{dukesNumberBean.maximum}.
Can you guess it?
</h2>
<p>
<h:inputText
id="userNo"
title="Type a number from 0 to 10:"
value="#{userNumberBean.userNumber}">
<f:validateLongRange
minimum="#{dukesNumberBean.minimum}"
maximum="#{dukesNumberBean.maximum}"/>
</h:inputText>
<h:commandButton id="submit" value="Submit" >
<f:ajax execute="userNo" …Run Code Online (Sandbox Code Playgroud) java-ee-7 ×10
cdi ×4
java ×3
ejb ×2
glassfish ×2
glassfish-4 ×2
java-ee ×2
ejb-3.2 ×1
gson ×1
jakarta-ee ×1
java-ee-6 ×1
jsf ×1
json ×1
jsonobject ×1
linux ×1
servlets ×1
spring ×1
spring-mvc ×1
ubuntu-12.04 ×1
war ×1
weld ×1