Ibm mobilefirst CDI不';不能在简单的适配器中工作
我已将CDI功能添加到server.xml文件Ibm mobilefirst CDI不';不能在简单的适配器中工作,ibm-mobilefirst,cdi,websphere-liberty,mobilefirst-adapters,open-liberty,Ibm Mobilefirst,Cdi,Websphere Liberty,Mobilefirst Adapters,Open Liberty,我已将CDI功能添加到server.xml文件CDI-1.2 MyMaven模块在/src/main/resources/META-INF文件夹中包含beans.xml 这是beans.xml内容: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-i
CDI-1.2
MyMaven模块在/src/main/resources/META-INF
文件夹中包含beans.xml
这是beans.xml内容:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
version="1.1" bean-discovery-mode="all">
</beans>
编辑:
package ch.webapp.presentation;
...
@Path("/test/")
public class MyController {
@Inject
private MyService myService;
@GET
@Path("/foo/{count}")
@OAuthSecurity(scope = "login")
@Produces("application/json")
public Response news(@PathParam("count") int count) {
return Response
.ok(myService.getBar(count))
.build();
}
}
Launching mfp (WebSphere Application Server 8.5.5.8/wlp-1.0.11.cl50820151201-1942) on Java HotSpot(TM) 64-Bit Server VM, version 1.8.0_172-b11 (en_CH)
Console Product version: 8.0.0.00-20180717-175523
那是我的豆子
package ch.webapp.service;
...
@RequestScoped
public class MyService {
public String getBar(int count) {
return "foo";
}
}
我通过扩展MFPJAXRSApplication
类初始化jax-rs
package ch.webapp;
...
public class AccountApplication extends MFPJAXRSApplication {
@Override
protected void init() throws Exception {
}
@Override
protected void destroy() throws Exception {
}
@Override
protected String getPackageToScan() {
return getClass().getPackage().getName();
}
}
环境详细信息:
package ch.webapp.presentation;
...
@Path("/test/")
public class MyController {
@Inject
private MyService myService;
@GET
@Path("/foo/{count}")
@OAuthSecurity(scope = "login")
@Produces("application/json")
public Response news(@PathParam("count") int count) {
return Response
.ok(myService.getBar(count))
.build();
}
}
Launching mfp (WebSphere Application Server 8.5.5.8/wlp-1.0.11.cl50820151201-1942) on Java HotSpot(TM) 64-Bit Server VM, version 1.8.0_172-b11 (en_CH)
Console Product version: 8.0.0.00-20180717-175523
出什么问题了?首先,WebSphereJAX-rs实现似乎不会自动集成jax-rs资源,除非您对它们进行适当的注释 通过适当的注释将jax-rs放在CDI管理的上下文中
@Path("/test/")
@javax.enterprise.context.RequestScoped
public class MyController {
@Inject
private MyService myService;
@GET
@Path("/foo/{count}")
@OAuthSecurity(scope = "login")
@Produces("application/json")
public Response news(@PathParam("count") int count) {
return Response
.ok(myService.getBar(count))
.build();
}
}
还要确保用于您的服务的注释是
@javax.enterprise.context.RequestScoped
这是一个典型的错误。CDI适用于托管的bean(例如EJB和servlet)。如果要在JAXRSBean上启用它,必须将其设置为“托管”,即将MyController注释为(例如javax.annotation.ManagedBean
或javax.ejb.Stateless
)
还要注意,对于webapp(.war),beans.xml文件必须位于WEB-INF文件夹中 根据您提供的输入,请检查以下清单
beans.xml
放在该路径src/main/resources/WEB-INF/beans.xml
中。(如果这是Java EE 7应用程序,那么beans.xml是可选的ch.webapp.presentation
@凌驾
受保护的字符串getPackageToScan(){
返回“ch.webapp.presentation”;
}MyService
是定义为cdibean还是具有显式生产者(@products
)方法?(要将MyService
定义为cdibean,只需将@ApplicationScoped
或@RequestScoped
放在MyService
类上即可)MyService
类被注释为@Stateless
您确定为JAX-RS资源激活了CDI吗?它可能需要一些额外的配置(我不知道,因为我不知道Websphere).是MyService
,还是@RequestScoped
?两者都是没有多大意义的!它是否与MyController
位于同一个jar中?如果不是,jar中是否有包含MyService
的beans.xml?还要确保@RequestScoped
是javax.enterprize.context
,并且没有不是javax.faces.bean下的那一个!你是否以通常的方式直接通过http客户端访问资源?我想确定你不是在自己构造MyController。你读过关于ManagedBean注释的哪一篇教程?我看过的每一篇教程都没有这样做。即使是最初的“oracle”教程也说明要做什么是在另一个答案中完成的(或者像你那样添加无状态),这是教程的问题。他们给出了一些示例,但没有清楚地解释原理。如果你想将bean注入bean,你的bean必须是“cdi感知的”。JAX-RS bean(参考资料)默认情况下不是CDIBean,您必须添加注释以允许注入。您有许多不同的注释(无状态、有状态、单例、RequestScope、ApplicationScope等),其中一个(实际上最基本的)是javax.annotation.ManagedBean。请阅读文档():“ManagedBean注释标记POJO(普通的旧Java对象)作为ManagedBean。ManagedBean支持一小部分基本服务,如“资源注入”。