我正在使用 tomee 服务器来运行我的 javaEE 应用程序。我写了一个注入对象的过滤器。但是该对象似乎没有被实例化:
以下是我的代码:
过滤器.java
@Priority(value = 2)
@Provider
@Singleton
public class Filter implements ContainerRequestFilter {
@Inject
private Faculty faculty;
public void filter(ContainerRequestContext requestContext) {
System.out.println("faculty name is :"+faculty.getFacultyName());
}
}
教员.java
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
public class Faculty {
private List<String> facultyMembers;
private String facultyName;
@PostConstruct
public void initialize() {
this.facultyMembers = new ArrayList<String>();
facultyMembers.add("Ian Schultz");
facultyMembers.add("Diane Reyes");
facultyName = "Computer Science";
}
public List<String> getFacultyMembers() {
return facultyMembers;
}
public String getFacultyName() {
return facultyName;
}
}
我正在获得 NPE。以下是堆栈跟踪:
java.lang.NullPointerException
at com.xyz.commerceapp.filters.Filter.filter(TransactionStartFilter.java:45)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.runContainerRequestFilters(JAXRSUtils.java:1645)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.processRequest(JAXRSInInterceptor.java:201)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.handleMessage(JAXRSInInterceptor.java:77)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
以下是正在使用的工件的版本:
<openejb.version>5.0.0-SNAPSHOT</openejb.version>
<tomee.version>2.0.0-SNAPSHOT</tomee.version>
我已经尝试在 META-INF 和 WEB-INF 中包含 beans.xml,但我仍然看到 NPE。有人可以让我知道我做错了什么吗?