【发布时间】:2018-02-03 23:33:57
【问题描述】:
我正在使用 Wildfly 10,Jersey,并使用 @Inject 注入依赖项。我有 DAO 和 Service 接口及其在 CustomBinder 中声明的实现。注入效果很好,但 EntityManager 被注入 null 并带有 @PersistenceContext 注释。我正在使用 MySQL,并且数据源测试连接正在工作。
API 休息类
@Path("/account")
public class CuentaServiceRS {
@Inject
private ICuentaService cuentaService;
@GET
@Produces(MediaType.APPLICATION_JSON)
public Cuenta getCuenta() {
return cuentaService.getCuentas().get(0);
}
}
ICuentaService
的实现@Stateless
public class CuentaServiceImpl implements ICuentaService {
@Inject
private ICuentaDAO cuentaDAO;
@Override
public List<Cuenta> getCuentas() {
List<Cuenta> cuentas = cuentaDAO.getAllCuentas();
cuentas;
}
}
CuentaDAO
的实现@Stateless
public class CuentaDAOImpl implements ICuentaDAO {
@PersistenceContext(unitName = "altitudePU")
protected EntityManager em;
@Override
public List<Cuenta> getAllCuentas() {
CriteriaQuery<Cuenta> cq = em.getCriteriaBuilder().createQuery(Cuenta.class);
/...
return resultlist;
}
}
我在 persistence.xml
中的持久性单元<persistence-unit name="altitudePU">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/AltitudePU</jta-data-source>
<class>ar.com.olx.domain.Cuenta</class>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>
web.xml
上配置的servlet<servlet>
<servlet-name>altitudeservlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>ar.com.villat.bind.ApplicationJaxRS</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
我的自定义 AplicationJaxRS 扩展自 ResourceConfig
public class ApplicationJaxRS extends ResourceConfig {
public ApplicationJaxRS(){
register(new CustomBinder());
packages(true, "ar.com.olx");
}
}
CustomBinder
public class CustomBinder extends AbstractBinder {
@Override
protected void configure() {
bind(CuentaServiceImpl.class).to(ICuentaService.class);
bind(CuentaDAOImpl.class).to(ICuentaDAO.class);
}
}
最后,我的 pom.xml 与这篇文章相关的依赖项
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.22.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.ejb</groupId>
<artifactId>jboss-ejb-api_3.2_spec</artifactId>
<version>1.0.0.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
<scope>provided</scope>
</dependency>
如果您需要更多信息,请告诉我。
【问题讨论】:
标签: java maven wildfly entitymanager jta