在 JBoss AS 7.1.1 上部署我的应用程序时,我遇到了 EntityManager 注入问题。应用程序使用 Struts2 和 Struts2 CDI 插件。除了 EntityManager 注入 CDI 工作正常 - 只有 EntityManager 没有注入。此错误特定于 JBoss - 在 GlassFish 3.1.2.2 Web Profile 下,相同的应用程序可以正常工作。应用程序与位于 WEB-INF/lib 文件夹中的分离 core-0.2.jar(应用程序逻辑)打包为 war。我不使用 Spring 等。
堆栈跟踪:
20:07:52,219 ERROR [org.apache.struts2.dispatcher.Dispatcher] (http--127.0.0.1-8090-1) Exception occurred during processing request: null: java.lang.NullPointerException
at proj.SystemUserRepositoryImpl.fetchByLogin(SystemUserRepositoryImpl.java:23) [core-0.2.jar:]
at proj.SystemUserServiceImpl.getCurrentUserInfo(SystemUserServiceImpl.java:31) [core-0.2.jar:]
at proj.web.GetAction.execute(GetAction.java:60) [classes:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_40]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_40]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_40]
at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_40]
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:252) [xwork-core-2.3.14.3.jar:2.3.14.3]
at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256) [struts2-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265) [xwork-core-2.3.14.3.jar:2.3.14.3]
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68) [struts2-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:249) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:249) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73) [struts2-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91) [struts2-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:252) [struts2-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164) [struts2-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187) [xwork-core-2.3.14.3.jar:2.3.14.3]
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246) [xwork-core-2.3.14.3.jar:2.3.14.3]
at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54) [struts2-core-2.3.14.3.jar:2.3.14.3]
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:546) [struts2-core-2.3.14.3.jar:2.3.14.3]
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77) [struts2-core-2.3.14.3.jar:2.3.14.3]
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91) [struts2-core-2.3.14.3.jar:2.3.14.3]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
at java.lang.Thread.run(Thread.java:724) [rt.jar:1.7.0_40]
项目结构:
core -> core-0.2.jar
src
main
java
proj
SystemUserServiceImpl.java
SystemUserRepositoryImpl.java
...
resources
META-INF
beans.xml
persistence.xml
...
web -> web.war
src
main
java
proj.web
GetAction.java
...
resources
META-INF
beans.xml
webapp
WEB-INF
web.xml
...
...
...
GetAction.java:
@Action("get/{id}")
public class GetAction extends ActionSupport {
@Inject
private SystemUserService systemUserService;
public SystemUserService getSystemUserService() {
return systemUserService;
}
public void setSystemUserService(SystemUserService systemUserService) {
this.systemUserService = systemUserService;
}
@Override
public String execute() throws Exception {
CurrentSystemUserInfo currentUserInfo = systemUserService.getCurrentUserInfo();
...
SystemUserServiceImpl.java:
@Named
public class SystemUserServiceImpl implements SystemUserService {
@Inject
private SystemUserRepository systemUserRepository;
public SystemUserRepository getSystemUserRepository() {
return systemUserRepository;
}
public void setSystemUserRepository(SystemUserRepository systemUserRepository) {
this.systemUserRepository = systemUserRepository;
}
@Override
@TransactionAttribute(value = TransactionAttributeType.REQUIRED)
public CurrentSystemUserInfo getCurrentUserInfo() {
SystemUser currentUserData = systemUserRepository.fetchByLogin(SystemUser.DEFAULT_LOGIN);
...
SystemUserServiceImpl.java:
public class SystemUserRepositoryImpl implements SystemUserRepository {
private EntityManager entityManager;
public EntityManager getEntityManager() {
return entityManager;
}
@PersistenceContext(unitName="MyApp")
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
@Override
public SystemUser fetchByLogin(String login) {
Query query = entityManager.createQuery("SELECT u FROM SystemUser u WHERE login = :login");
...
持久性.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="MyApp" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/jdbc/myapp</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.dialect" value="proj.DerbyDialect"/>
<property name="hibernate.ejb.naming_strategy" value="proj.DatabaseStructureNamingStrategy"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
JBoss 7.1.1 配置:
<subsystem xmlns="urn:jboss:domain:datasources:1.0">
<datasources>
<datasource jndi-name="java:jboss/jdbc/myapp" pool-name="MyApp">
<connection-url>jdbc:derby://localhost/myappdb;create=true</connection-url>
<driver>derby</driver>
<security>
<user-name>myappu</user-name>
<password>myappp</password>
</security>
</datasource>
<drivers>
<driver name="derby" module="org.apache.derby">
<driver-class>org.apache.derby.jdbc.ClientDriver</driver-class>
<xa-datasource-class>org.apache.derby.jdbc.ClientXADataSource</xa-datasource-class>
</driver>
</drivers>
</datasources>
</subsystem>
我做错了什么?任何帮助都会得到帮助。