1

我有这样的道实现

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 意识到手动创建的实例?

4

2 回答 2

1

您可以使用 AspectJ 使用 Spring AOP 支持来做到这一点。在这里阅读更多:http: //static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/aop.html#aop-atconfigurable

启用此功能后,Spring 将知道使用 Configurable 注释注释的类创建的任何实例。然后 Spring 将能够识别 PreAuthorize 注释。

于 2013-05-28T09:06:23.493 回答
0

为什么需要工厂来创建 DAO?这就是 Spring 应用程序上下文。

您看起来想限制使用基于角色的安全性调用 DAO 方法的能力。我认为这很好,并且可以做到,但您不必限制 DAO 的创建。使用 Spring 创建它,然后限制访问。你的方式是矫枉过正和不必要的。

于 2013-05-27T14:08:15.697 回答