我有DI的Guice。 我可以在控制器中注入我的存储库(构造函数注入):
@ImplementedBy(JPAMyEntityRepository.class)
public interface MyEntityRepository extends EntityRepository<MyEntity> {
}
@Singleton
public class JPAMyEntityRepository implements MyEntityRepository {
.....
}
public class MyEntityController extends BaseController {
private MyEntityRepository myEntityRepository;
@Inject
public MyEntityController(MyEntityRepository myEntityRepo) {
this.myEntityRepository = myEntityRepo; //OK, repo is not null
}
}
但我无法使用构造函数在我的实体侦听器中注入相同的存储库,我有一个例外(没有默认构造函数):
@MappedSuperclass
@EntityListeners(MyEntityListener.class)
public abstract class MyEntity extends OwnedEntity implements Comparable<MyEntity> {
....
}
public class MyEntityListener {
private MyEntityRepository myEntityRepository;
@Inject
public MyEntityListener(MyEntityRepository myEntityRepo){
this.myEntityRepository = myEntityRepo;
}
}
例外:
....
Caused by: javax.persistence.PersistenceException: Unable to create instance of models.listeners.MyEntityListener as a JPA callback listener
....
Caused by: java.lang.NoSuchMethodException: models.listeners.MyEntityListener.<init>()
当我尝试以这种方式注入我的回购时:
public class MyEntityListener {
@Inject
private MyEntityRepository myEntityRepository; // null
}
我有一个空。
Build.sbt文件:
import MetamodelGenerator.generateMetamodels
name := """my-app"""
version := "1.2.4-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayJava).settings(sourceGenerators in Compile += generateMetamodels(Seq("models")).taskValue)
scalaVersion := "2.12.2"
libraryDependencies ++= Dependencies.projectDependencies
application.conf文件:
# JDBC
db {
default {
driver = org.postgresql.Driver
url = "jdbc:postgresql://localhost:5432/my-app"
schema = myapp
username = "user"
password = "user"
jndiName = DefaultDS
logSql = false
hikaricp {
connectionTestQuery = SELECT 1
minimumIdle = 20
maximumPoolSize = 40
}
}
}
# Point JPA at our database configuration
jpa.default = defaultPersistenceUnit
# Job queue sized to HikariCP connection pool
database.dispatcher {
executor = "thread-pool-executor"
throughput = 1
thread-pool-executor {
fixed-pool-size = 40
}
}