gpt4 book ai didi

java - 如何远程访问@Stateless @LocalBean

转载 作者:行者123 更新时间:2023-11-29 03:17:28 25 4
gpt4 key购买 nike

我正在关注 EJB cookbook ,来自 packt,使用以下代码:

package packt;

import javax.ejb.Stateless;
import javax.ejb.LocalBean;

//@Stateless
@LocalBean
@Stateless(mappedName="salutationBean")
public class Salutation {
public String getFormalSalutation(String name) {
return "Dear " + name;
}
public String getInformalSalutation(String name) {
return "Hi " + name;
}
}

我如何从不扩展 HttpServlet 的类访问这个 bean? servlet 本身工作正常:

package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
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 packt.Salutation;

@WebServlet(urlPatterns = {"/SalutationServlet"})
public class SalutationServlet extends HttpServlet {

@EJB
private Salutation salutation;

protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet SalutationServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>"
+ salutation.getFormalSalutation("Sherlock Holmes")
+ "</h1>");
out.println("</body>");
out.println("</html>");
} finally {
out.flush();
out.close();
}
}

@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}

并且可以从指定的 URL 查看:

thufir@dur:~$ 
thufir@dur:~$ lynx http://localhost:8080/SalutationApplication-war/SalutationServlet -dump
Dear Sherlock Holmes


thufir@dur:~$

当前结构:

thufir@dur:~/NetBeansProjects$ 
thufir@dur:~/NetBeansProjects$ tree SalutationApplication/
SalutationApplication/
├── build.xml
├── nbproject
│   ├── ant-deploy.xml
│   ├── build-impl.xml
│   ├── genfiles.properties
│   ├── private
│   │   └── private.properties
│   ├── project.properties
│   └── project.xml
├── SalutationApplication-ejb
│   ├── build.xml
│   ├── nbproject
│   │   ├── ant-deploy.xml
│   │   ├── build-impl.xml
│   │   ├── genfiles.properties
│   │   ├── private
│   │   │   └── private.properties
│   │   ├── project.properties
│   │   └── project.xml
│   └── src
│   ├── conf
│   │   └── MANIFEST.MF
│   └── java
│   └── packt
│   └── Salutation.java
├── SalutationApplication-war
│   ├── build.xml
│   ├── nbproject
│   │   ├── ant-deploy.xml
│   │   ├── build-impl.xml
│   │   ├── genfiles.properties
│   │   ├── private
│   │   │   └── private.properties
│   │   ├── project.properties
│   │   └── project.xml
│   ├── src
│   │   ├── conf
│   │   │   └── MANIFEST.MF
│   │   └── java
│   │   └── servlet
│   │   └── SalutationServlet.java
│   └── web
│   ├── index.html
│   └── WEB-INF
└── src
└── conf
└── MANIFEST.MF

20 directories, 27 files
thufir@dur:~/NetBeansProjects$

现在它只是一个本地 bean 很好,但最终我希望 Salutation.java bean 可以远程访问。我从 Head First EJB 对 EJB 有一定的了解。 ,但那已经很老了。

最佳答案

(这个答案与我上面的评论有关。)

在容器未自动管理的对象中,您可以通过获取对 BeanManager 的引用来初始化手动注入(inject),并让 CDI 容器为您完成所有工作。这适用于您还可以注入(inject)托管 bean 的所有内容,但您不必进行复杂的 JNDI 查找。

public class CDIUtils {

public static BeanManager beanManager() {
try {
return (BeanManager) new InitialContext().lookup("java:comp/BeanManager");
} catch (NamingException e) {
throw new IllegalStateException("Unable to obtain CDI BeanManager", e);
}
}

public static void makeManagedStatic(Object obj) {
BeanManager bm = beanManager();
makeManaged(obj, bm);
}

public static void makeManaged(Object obj, BeanManager beanManager) {
Class objClass = (Class) obj.getClass();
AnnotatedType annotatedType = beanManager.createAnnotatedType(objClass);
InjectionTarget injectionTarget = beanManager.createInjectionTarget(annotatedType);
CreationalContext context = beanManager.createCreationalContext(null);
injectionTarget.inject(obj, context);
}

}

假设这是您的客户:

public class MyClient {
@EJB private MyEJB myEJB;
public void run() {
if (myEJB == null)
CDIUtils.makeManagedStatic(this);
myEJB.someMethod();
}
}

您也可以简单地让它们在创建时被管理,这样可以避免对 null 的测试:

MyClient client = new MyClient();
CDIUtils.makeManagedStatic(client);
client.run(); // etc

关于java - 如何远程访问@Stateless @LocalBean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25700311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com