我有这样的道实现
public class EntityDao<T> {
private Class clazz;
private SessionFactory sessFactory;
public EntityDao(Class clazz, SessionFactory sessFactory) {
this.clazz = clazz;
this.sessFactory = sessFactory;
}
.... dao methods
}
以及用于检索和存储特定 dao 的工厂
EntityBeanDaoFactory {
private HashMap<EntityDaoType, EntityDao> daoMap = new HashMap<EntityDaoType, EntityDao>();
// return dao from daoMap if exists a if not create it and put it in the map then return dao
public EntityDao createDao(EntityDaoType entityType) {
switch (entityType) {
case mySpecialDaoTYPE:
if (!daoMap.containsKey(entityType)) {
EntityDao<Type> mySpecialDao = new EntityDao(Type.class, sessFactory);
daoMap.put(entityType, mySpecialDao);
}
return daoMap.get(entityType);
}
}
现在我想用 @PreAuthorize("hasPermission()") 注释 dao 方法,但是 spring 不知道以这种方式创建的 daos,我无法立即重构整个项目,所以我创建了 dao,我需要这样做在 aplicationContectxt.xml 中使用注释
<bean id="mySpecialDao" class="..EntityDao" >
<constructor-arg>
<value>myClass</value>
</constructor-arg>
<constructor-arg ref="sessionFactory" />
</bean>
在工厂内部,我有改变行为来创建这样的特定 dao
if (!daoMap.containsKey(entityType)) {
EntityDao<Class> dao = (EntityDao<Class>) AppContext.getApplicationContext().getBean("mySpecialDao");
daoMap.put(entityType, dao);
}
有没有更好的方法让 spring 了解我的 DAO?我的意思是有没有办法让 Spring 意识到手动创建的实例?