浏览 1865 次
锁定老帖子 主题:一种非常蹩脚的万用泛型service实现
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2012-01-20
最后修改:2012-01-20
@Service public class PubManager<T, PK extends Serializable>{ private SessionFactory sessionFactory; @Autowired private PubManagerImpl<T, PK> mgr; @Autowired public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public PubManagerImpl<T, PK> getI(Class<T> c){ if(null == mgr.getDAO()){ mgr.setDao(new HibernateDao<T, PK>(sessionFactory ,c)); } return mgr; } } @Service @Transactional public class PubManagerImpl<T, PK extends Serializable> extends AbsManager<T, PK> implements absMgr<T, PK> { private HibernateDao<T, PK> dao; @Override public HibernateDao<T, PK> getDAO() { return dao; } public void setDao(HibernateDao<T, PK> dao) { this.dao = dao; } } @Controller @RequestMapping("/xxx/xxxx") public class XxxxXxxxXxxController { @Autowired private CodeManager codeManager; @Autowired private PubManager<XxxxxXxxxXxxx, String> pubManager; @RequestMapping("allXxx") public @ResponseBody Page<XxxxxXxxxXxxx> logs(){ return pubManager.getI(XxxxxXxxxXxxx.class).searchByPage(); } } 需要获得运行时的Class<T> 感觉上是可以做到样子…… 参照泛型DAO的写法 完全不行 人家也不是万用实现 求打通七筋八脉~ 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2012-01-20
PubManager有错误,不同的泛型居然是单例的,没想到 @Service public class PubManager<T, PK extends Serializable>{ private SessionFactory sessionFactory; private PubManagerImpl<T, PK> mgr; private Map<Class<T>, HibernateDao<T, PK>> daoMap = new HashMap<Class<T>, HibernateDao<T, PK>>(); @Autowired public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } @Autowired public void setPubManagerImpl(PubManagerImpl<T, PK> mgr) { this.mgr = mgr; } public PubManagerImpl<T, PK> getI(Class<T> c){ if(!daoMap.containsKey(c)) daoMap.put(c, new HibernateDao<T, PK>(sessionFactory ,c)); mgr.setDao(daoMap.get(c)); return mgr; } } |
|
返回顶楼 | |