我試圖讓CDI在我的應用程序中工作,但是我只做到了NullPointerExceptions。TomEE CDI @Inject NullPointerException
當正常實例化播放列表服務,它工作得很好,但是當我嘗試使用CDI時,它不再。
服務器能夠啓動,但是當我將beans.xml添加到META-INF /和WEB-INF /時,它也無法啓動。
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>
編輯:
資源/ 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>
beans.xml的是在Java EE 7中是可選的。將javaee-api的作用域更改爲提供。 – fg78nc