在使用@EJB注释注入bean时,我遇到了问题--bean总是空的.
首先,这是我的项目结构的相关部分。
ApplEAR.ear
|
├──WebModule.war (de.example.rest)
└──EJBModule.war (de.example.service)所以..。现在,我正尝试在我的now模块中从ejbmodule注入一个bean:
de.example.rest.RestBean:
@Stateless(name = "RestBean")
@Path("/restElements")
public class RestBean implements IRestBean {
@EJB
private UserBean userBean;de.example.service.UserBean:
@Stateless(name = "UserBean")
@TransactionManagement(TransactionManagementType.BEAN)
@LocalBean
public class UserBean{...在我的WebModule中,我有这个类来扩展应用程序类:
@ApplicationPath("/*")
public class RestApplication extends javax.ws.rs.core.Application {
/**
* {@inheritDoc}
*/
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(de.example.rest.RestBean.class);
return classes;
}
}ear部署在具有以下已安装功能的websphere these服务器上:
<feature>localConnector-1.0</feature>
<feature>appSecurity-3.0</feature>
<feature>beanValidation-2.0</feature>
<feature>ejbLite-3.2</feature>
<feature>ejbRemote-3.2</feature>
<feature>javaMail-1.6</feature>
<feature>jca-1.7</feature>
<feature>jdbc-4.2</feature>
<feature>jndi-1.0</feature>
<feature>jpaContainer-2.2</feature>
<feature>jsf-2.3</feature>
<feature>jsp-2.3</feature>
<feature>ldapRegistry-3.0</feature>
<feature>servlet-4.0</feature>
<feature>transportSecurity-1.0</feature>
<feature>websocket-1.1</feature>
<feature>jaxws-2.2</feature>
<feature>jaxrs-2.1</feature>我尝试了不同的方法来解决我的问题:在每个WEB/META文件夹中创建一个beans.xml,使用@LocalBean和一个@LocalBean来尝试,然后使用@EJB(查找=beans.xml使用@Inject )进行注入。
在的帮助下
InitialContext ctx = new InitialContext();
Object service = ctx.lookup(java:global/ApplEAR/EJBModule/UserBean!de.example.service.UserBean);我可以查找bean,但是使用注释会更容易。
我错过了什么或者做错了什么?
发布于 2020-10-21 20:49:40
我将在没有解释的情况下给出一个答案(因为我无法描述JAX中EJB与CDI注入的所有细微之处、规范等)。
您可以将UserBean注入到RestBean中,方法是从RestBean中删除EJB注释,并添加@Dependent将其转换为CDI管理的bean。
// ...
import javax.enterprise.context.Dependent;
//@Stateless(name = "RestBean")
@Path("/restElements")
@Dependent
public class RestBean implements IRestBean {这假设您不需要使用RestBean作为远程EJB,并且只将它用作CDI管理的bean和/或JAX-RS资源。
(Note:这假设您启用了CDI功能,并以过渡/间接方式实现)。
https://stackoverflow.com/questions/64349641
复制相似问题