TomEE CDI @Inject NullPointerException
TomEE CDI @Inject NullPointerException
我正在尝试让 CDI 在我的应用程序中工作,但是当我这样做时我只得到 NullPointerExceptions。
当正常实例化 playlistService 时,它工作得很好,但是当我尝试使用 CDI 时,它不再工作了。
服务器可以启动,但是当我在 META-INF/ 和 WEB-INF/ 中添加 beans.xml 时,它也无法启动了。
PlaylistRESTController.java
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
@Path("/api/playlist")
public class PlaylistRESTController {
@Inject
PlaylistService playlistService; //This one
@GET
@Produces(MediaType.APPLICATION_JSON)
public Playlist[] getUsersPlaylists(
@CookieParam("st-session") Cookie session,
@Context HttpServletRequest httpServletRequest
) {
User user = (User) httpServletRequest.getAttribute("user");
Playlist[] playlist = playlistService.getAllPlaylists(user);
return playlist;
}
}
PlaylistService.java
public class PlaylistService implements IPlaylistService {
PlaylistJPA playlistJPA = new PlaylistJPA();
public Playlist[] getAllPlaylists(User owner) {
ArrayList<Playlist> playlist = this.playlistJPA.findByOwner(owner);
return playlist.toArray(new Playlist[playlist.size()]);
}
}
pom.xml
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.2.6.Final</version>
</dependency>
</dependencies>
编辑:
resources/META-INF/persistence.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="mysql">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>
nl.han.dea.domain.Song
</class>
<class>
nl.han.dea.domain.Playlist
</class>
<class>
nl.han.dea.domain.Video
</class>
<class>
nl.han.dea.domain.User
</class>
<properties>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.cj.jdbc.Driver"/>
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost:3306/Spotitube?autoReconnect=true&useSSL=false&serverTimezone=UTC"/>
<property name="hibernate.connection.username" value="spotitube"/>
<property name="hibernate.connection.password" value="java"/>
<property name="hibernate.show_sql" value="true"/>
<property name="javax.persistence.sql-load-script-source" value="insert.sql"/>
<property name="hibernate.flushMode" value="FLUSH_AUTO"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
你只需要在WEB-INF/
中添加一个beans.xml
还要确保 javaee-api 具有提供的范围,确保从 hibernate-entitymanager 传递依赖项中排除 jpa 和 jta api 以确保您的 war 具有正确的依赖项并且没有与 EE 服务器冲突 API。
旁注:使用 org.apache.tomee:javaee-api:7.0-1:provided 而不是 javax:javaee-api 也可以使嵌入式测试工作(openejb 或嵌入 tomee)
我正在尝试让 CDI 在我的应用程序中工作,但是当我这样做时我只得到 NullPointerExceptions。
当正常实例化 playlistService 时,它工作得很好,但是当我尝试使用 CDI 时,它不再工作了。
服务器可以启动,但是当我在 META-INF/ 和 WEB-INF/ 中添加 beans.xml 时,它也无法启动了。
PlaylistRESTController.java
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
@Path("/api/playlist")
public class PlaylistRESTController {
@Inject
PlaylistService playlistService; //This one
@GET
@Produces(MediaType.APPLICATION_JSON)
public Playlist[] getUsersPlaylists(
@CookieParam("st-session") Cookie session,
@Context HttpServletRequest httpServletRequest
) {
User user = (User) httpServletRequest.getAttribute("user");
Playlist[] playlist = playlistService.getAllPlaylists(user);
return playlist;
}
}
PlaylistService.java
public class PlaylistService implements IPlaylistService {
PlaylistJPA playlistJPA = new PlaylistJPA();
public Playlist[] getAllPlaylists(User owner) {
ArrayList<Playlist> playlist = this.playlistJPA.findByOwner(owner);
return playlist.toArray(new Playlist[playlist.size()]);
}
}
pom.xml
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.2.6.Final</version>
</dependency>
</dependencies>
编辑:
resources/META-INF/persistence.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="mysql">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>
nl.han.dea.domain.Song
</class>
<class>
nl.han.dea.domain.Playlist
</class>
<class>
nl.han.dea.domain.Video
</class>
<class>
nl.han.dea.domain.User
</class>
<properties>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.cj.jdbc.Driver"/>
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost:3306/Spotitube?autoReconnect=true&useSSL=false&serverTimezone=UTC"/>
<property name="hibernate.connection.username" value="spotitube"/>
<property name="hibernate.connection.password" value="java"/>
<property name="hibernate.show_sql" value="true"/>
<property name="javax.persistence.sql-load-script-source" value="insert.sql"/>
<property name="hibernate.flushMode" value="FLUSH_AUTO"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
你只需要在WEB-INF/
中添加一个beans.xml还要确保 javaee-api 具有提供的范围,确保从 hibernate-entitymanager 传递依赖项中排除 jpa 和 jta api 以确保您的 war 具有正确的依赖项并且没有与 EE 服务器冲突 API。
旁注:使用 org.apache.tomee:javaee-api:7.0-1:provided 而不是 javax:javaee-api 也可以使嵌入式测试工作(openejb 或嵌入 tomee)