我对Java中的@Inject感到很困惑。我在Java EE教程中读过关于CDI的内容,但我还是没有得到它。你会看看我的代码并告诉我我做错了什么吗?
package model.businessLayer;
import java.util.List;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import model.domainLayer.RADplusUserSupplemental;
@Named(value = "staffLocal")
public class StaffCaseloadMgr implements StaffCaseloadMgrLocal {
@PersistenceContext(unitName = "CareTeamPersist")
EntityManager em;
public StaffCaseloadMgr() {}
/**
* Returns the list of user supplemental records from the database,
* based on the patid passed in to staff caseload.
* @param patid The patient, or consumer ID
* @return List of RADplusUserSupplemental records
*/
public List<RADplusUserSupplemental> getListForEmails(String patid) {
return em.createNamedQuery("StaffCaseload.findEmails",
RADplusUserSupplemental.class)
.getResultList();
}
}
我把构造函数放在那里,认为它可能会有所帮助。我不知道为什么它会看到它是如何构成默认构造函数的。好的,接下来。
package utils;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import model.businessLayer.StaffCaseloadMgr;
import model.domainLayer.RADplusUserSupplemental;
@SessionScoped
public class CareTeam implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
static StaffCaseloadMgr staffLocal;
/**
* This is a static method that retrieves the care team given a particular
* consumer ID.
*
* @param consumerID
* The consumer ID for which to get the care team
*
* @return A list of emails for the care team
*/
public static List<Map<String, String>> getCareTeamEmails(String consumerID) {
List<Map<String, String>> arrayList = new ArrayList<Map<String, String>>();
for (RADplusUserSupplemental userSupp : staffLocal
.getListForEmails(consumerID)) {
Map<String, String> emailMap = new HashMap<String, String>();
emailMap.put("staffEmail", userSupp.getOrganizationEmailAddress());
arrayList.add(emailMap);
}
return arrayList;
}
}
我正在使用Maven,因此我在beans.xml
中添加了一个空的resources/META-INF
。我很困惑为什么它不起作用。你能帮忙吗?
我应该知道你无法读懂我的想法!对于那个很抱歉。我使用的是JBoss容器,我的application.xml
中有版本7。我想将我的JPA放在一个单独的.jar文件中,因为这个小函数将被几个不同的进程使用。
堆栈跟踪如下所示:
07:58:00,811 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/TestConfiguration-1.0].[rest]] (http-/172.29.8.11:443-1) JBWEB000236: Servlet.service() for servlet rest threw exception: java.lang.NullPointerException
at utils.CareTeam.getCareTeamEmails(CareTeam.java:43) [CareTeam-1.0.0.jar:]
at controller.TestConfiguration.testing(TestConfiguration.java:33) [classes:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_79]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_79]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_79]
at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_79]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) [spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) [spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:754) [jboss-servlet-api_3.0_spec-1.0.2.Final-redhat-1.jar:1.0.2.Final-redhat-1]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.2.Final-redhat-1.jar:1.0.2.Final-redhat-1]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:295) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:149) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.2.1.Final-redhat-10.jar:7.2.1.Final-redhat-10]
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.2.1.Final-redhat-10.jar:7.2.1.Final-redhat-10]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:169) [jboss-as-web-7.2.1.Final-redhat-10.jar:7.2.1.Final-redhat-10]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:145) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:97) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:102) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:336) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:653) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:920) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_79]
编辑2: 所以我接受了斯韦特林的建议并摆脱了静态&#34;在变量前面。我必须调用另一个包含@Inject的类,如下所示:
CareTeam.java
package utils;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
public class CareTeam implements Serializable {
private static final long serialVersionUID = 1L;
/**
* This is a static method that retrieves the care team given a particular
* consumer ID.
*
* @param consumerID
* The consumer ID for which to get the care team
*
* @return A list of emails for the care team
*/
public static List<Map<String, String>> getCareTeamEmails(String consumerID) {
CareTeamImpl careTeam = new CareTeamImpl();
return careTeam.getCareTeamEmails(consumerID);
}
}
CareTeamImpl.java
package utils;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import model.businessLayer.StaffCaseloadMgr;
import model.domainLayer.RADplusUserSupplemental;
@SessionScoped
public class CareTeamImpl implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
StaffCaseloadMgr staffLocal;
/**
* This is a static method that retrieves the care team given a particular
* consumer ID.
*
* @param consumerID
* The consumer ID for which to get the care team
*
* @return A list of emails for the care team
*/
public List<Map<String, String>> getCareTeamEmails(String consumerID) {
List<Map<String, String>> arrayList = new ArrayList<Map<String, String>>();
for (RADplusUserSupplemental userSupp : staffLocal
.getListForEmails(consumerID)) {
Map<String, String> emailMap = new HashMap<String, String>();
emailMap.put("staffEmail", userSupp.getOrganizationEmailAddress());
arrayList.add(emailMap);
}
return arrayList;
}
}
它仍然无法正常工作。它没有部署,但是:
11:49:45,624 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-18) MSC000001: Failed to start service jboss.deployment.unit."TestConfiguration-1.0.war".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."TestConfiguration-1.0.war".WeldStartService: Failed to start service
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1767) [jboss-msc-1.0.4.GA-redhat-1.jar:1.0.4.GA-redhat-1]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_79]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_79]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_79]
Caused by: org.jboss.weld.exceptions.UnserializableDependencyException: WELD-001413 The bean Managed Bean [class utils.CareTeamImpl] with qualifiers [@Any @Default] declares passivating scope but has non-serializable dependency Managed Bean [class model.businessLayer.StaffCaseloadMgr] with qualifiers [@Any @Default @Named]
at org.jboss.weld.bootstrap.Validator.validateInjectionPointPassivationCapable(Validator.java:360)
at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:331)
at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:280)
at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:143)
at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:163)
at org.jboss.weld.bootstrap.Validator.validateBeans(Validator.java:382)
at org.jboss.weld.bootstrap.Validator.validateDeployment(Validator.java:367)
at org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(WeldBootstrap.java:379)
at org.jboss.as.weld.WeldStartService.start(WeldStartService.java:64)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.4.GA-redhat-1.jar:1.0.4.GA-redhat-1]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.4.GA-redhat-1.jar:1.0.4.GA-redhat-1]
... 3 more
我没有做&#34;实现Serializable&#34;在CareTeam.java中的东西 - 我在RedHat客户门户网站上查看了一个建议在这种类型的错误上使其成为Serializable的东西 - 所以我把它放在那里并得到了相同的结果。现在我不知道该怎么想。