2013-05-27 18 views
1

我有道implentation這樣Spring和手動創建的DAO如何重構對這類DAO實現hasPermisions對象

public class EntityDao<T> { 

    private Class clazz; 
    private SessionFactory sessFactory; 

    public EntityDao(Class clazz, SessionFactory sessFactory) { 
     this.clazz = clazz; 
     this.sessFactory = sessFactory; 

    } 
.... dao methods 
} 

和工廠retriving和存儲特定的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> 

在工廠裏面我已經changet行爲創造這樣

 if (!daoMap.containsKey(entityType)) { 
     EntityDao<Class> dao = (EntityDao<Class>) AppContext.getApplicationContext().getBean("mySpecialDao"); 
       daoMap.put(entityType, dao); 
      } 

這個特殊的刀是有一些更好的辦法如何讓春天知道我的DAO的?我的意思是有辦法讓Spring知道手動創建的實例嗎?

回答

0

爲什麼你需要一個工廠來創建DAO?這就是Spring應用程序的上下文。

您看起來像是想限制使用基於角色的安全性調用DAO方法的能力。我認爲這很好,可以做,但你不需要限制DAO的創建。使用Spring創建它,然後限制訪問。你的方式是矯枉過正和不必要的。

+0

工廠allredy那裏,它是硬編碼在這麼多的地方,所以它的困難,以擺脫它(這不是我:-))。我不需要限制對dao或其方法的調用,而是實施基於curent用戶的某種ACL,目標實體ID,因此我擁有自己的permisionEvaluator(hasPermission(認證認證,可序列化targetId,字符串targetType,對象權限)) –