Spring 将实体传递给超级 class
Spring pass entity to super class
我创建了这样的通用超级class:
@Repository
public class RootQueryCreator<T> {
@PersistenceContext
private EntityManager em;
private T entity;
CriteriaBuilder criteriaBuilder = null;
CriteriaQuery<T> criteriaQuery = null;
Root<T> rootTable = null;
public RootQueryCreator() {}
public RootQueryCreator(T entity) {
super();
this.entity = entity;
}
@PostConstruct
public void initRootQuery() {
criteriaBuilder = em.getCriteriaBuilder();
criteriaQuery = (CriteriaQuery<T>)
criteriaBuilder.createQuery(this.entity.getClass());
rootTable = (Root<T>) criteriaQuery.from(entity.getClass());
}}
这个 superclass 将被实体中的每个 DAOImpl-Class 使用。
像这样:
@Repository
@Qualifier("myEntitiyClass")
public class MyEntityDAOImpl extends
RootQueryCreator<MyEntity> {
@Autowired
public MyEntityDAOImpl() {
super(new MyEntity()); //pass any Entity for the Root-Class from Criteria-Framework
}
@Override
public List<MyEntity> getAll() throws Exception {
super.getCriteriaQuery().select(super.rootTable);
return super.getEm().createQuery(criteriaQuery).getResultList();
}
public List<MyEntity> retrieveData(){
}}
并且每个 DAOImpl-Class 将被自动连接到一些服务-class 中,如下所示:
@Service
public class myLecture {
@Autowired
@Qualifier("myEntitiyClass")
private JpaRepositority<MyEntityDAOImpl> myEntityDAOImpl;
public void retrieveData(){
myEntityDAOImpl.retrieveData();
} }
最后我得到这个错误:
IllegalStateException: Autowired annotation requires at least one
argument: MyEntityDAOImpl()
实际上我不需要将任何实体传递给 DAOImpl-Class。
我该怎么办?
Autowired
注释指定:
Marks a constructor, field, setter method or config method as to be
autowired by Spring's dependency injection facilities.
但是你用 @Autowired
注释了构造函数而没有提供任何依赖项作为参数:
@Autowired
public MyEntityDAOImpl() { //-> Empty arg is the issue here
super(new MyEntity()); //pass any Entity for the Root-Class from Criteria-Framework
}
而例外情况:
IllegalStateException: Autowired annotation requires at least one
argument: MyEntityDAOImpl()
事实上,您不需要在 MyEntityDAOImpl
中自动装配任何内容,因为它不需要在其构造函数中有任何依赖关系。所以只需删除注释并让 Spring 将构造函数作为普通构造函数调用:
public MyEntityDAOImpl() {
super(new MyEntity()); //pass any Entity for the Root-Class from Criteria-Framework
}
我创建了这样的通用超级class:
@Repository
public class RootQueryCreator<T> {
@PersistenceContext
private EntityManager em;
private T entity;
CriteriaBuilder criteriaBuilder = null;
CriteriaQuery<T> criteriaQuery = null;
Root<T> rootTable = null;
public RootQueryCreator() {}
public RootQueryCreator(T entity) {
super();
this.entity = entity;
}
@PostConstruct
public void initRootQuery() {
criteriaBuilder = em.getCriteriaBuilder();
criteriaQuery = (CriteriaQuery<T>)
criteriaBuilder.createQuery(this.entity.getClass());
rootTable = (Root<T>) criteriaQuery.from(entity.getClass());
}}
这个 superclass 将被实体中的每个 DAOImpl-Class 使用。 像这样:
@Repository
@Qualifier("myEntitiyClass")
public class MyEntityDAOImpl extends
RootQueryCreator<MyEntity> {
@Autowired
public MyEntityDAOImpl() {
super(new MyEntity()); //pass any Entity for the Root-Class from Criteria-Framework
}
@Override
public List<MyEntity> getAll() throws Exception {
super.getCriteriaQuery().select(super.rootTable);
return super.getEm().createQuery(criteriaQuery).getResultList();
}
public List<MyEntity> retrieveData(){
}}
并且每个 DAOImpl-Class 将被自动连接到一些服务-class 中,如下所示:
@Service
public class myLecture {
@Autowired
@Qualifier("myEntitiyClass")
private JpaRepositority<MyEntityDAOImpl> myEntityDAOImpl;
public void retrieveData(){
myEntityDAOImpl.retrieveData();
} }
最后我得到这个错误:
IllegalStateException: Autowired annotation requires at least one argument: MyEntityDAOImpl()
实际上我不需要将任何实体传递给 DAOImpl-Class。 我该怎么办?
Autowired
注释指定:
Marks a constructor, field, setter method or config method as to be autowired by Spring's dependency injection facilities.
但是你用 @Autowired
注释了构造函数而没有提供任何依赖项作为参数:
@Autowired
public MyEntityDAOImpl() { //-> Empty arg is the issue here
super(new MyEntity()); //pass any Entity for the Root-Class from Criteria-Framework
}
而例外情况:
IllegalStateException: Autowired annotation requires at least one argument: MyEntityDAOImpl()
事实上,您不需要在 MyEntityDAOImpl
中自动装配任何内容,因为它不需要在其构造函数中有任何依赖关系。所以只需删除注释并让 Spring 将构造函数作为普通构造函数调用:
public MyEntityDAOImpl() {
super(new MyEntity()); //pass any Entity for the Root-Class from Criteria-Framework
}