Spring、OSGi整合Hibernate 二

接上文,为了测试已经搭好的架子,创建了一个注册实体的Bundle(wanged_security_entity),这个里面包含了两个实体类(Role、User)和它们的Hibernate映射文件(Role.hbm.xml、User.hbm.xml),以及一个实现了EntityRegister接口的类(SecurityEntityRegisterImpl)以提供注册实体的服务。这里仅将SecurityEntityRegisterImpl的代码列出如下:
java 代码
 
  1. package wanged.security.entity;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import wanged.core.persistent.entity.EntityRegister;  
  6.   
  7.   
  8. @SuppressWarnings("unchecked")  
  9. public class SecurityEntityRegisterImpl implements EntityRegister{  
  10.   
  11.     public Class[] register() {  
  12.         ArrayList<class> cList = new ArrayList<class>();  </class></class>
  13.          
  14.         cList.add(Role.class);  
  15.         cList.add(User.class);  
  16.           
  17.         return cList.toArray(new Class[cList.size()]);  
  18.     }  
  19.   
  20. }  
下面来声明该服务:
xml 代码
 
  1. <osgi:service interface="wanged.core.persistent.entity.EntityRegister">      
  2.   <bean class="wanged.security.entity.SecurityEntityRegisterImpl" />      
  3. </osgi:service>  
 这个服务就是上篇中,"wanged_core_persistent" Bundle中引用的服务,完成了实体类的注册。
       现在有了实体类,就需要有对实体类进行CURD操作的Bundle(wanged_security_service),这个Bundle中采用了常见的Service-DAO模式,以对Role的操作为例,包括RoleService:
java 代码
 
  1. package wanged.security.service;  
  2.   
  3. import java.util.List;  
  4.   
  5. import wanged.security.dao.RoleDao;  
  6. import wanged.security.entity.Role;  
  7.   
  8. public interface RoleService {  
  9.   
  10.     void setRoleDao(RoleDao rdao);  
  11.       
  12.     void saveRole(Role r);  
  13.       
  14.     List<role> findAll();  </role>
  15. }  
和其实现RoleServiceImpl:
java 代码
 
  1. package wanged.security.service.impl;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.springframework.transaction.annotation.Propagation;  
  6. import org.springframework.transaction.annotation.Transactional;  
  7.   
  8. import wanged.security.dao.RoleDao;  
  9. import wanged.security.entity.Role;  
  10. import wanged.security.service.RoleService;  
  11.   
  12. @Transactional(readOnly = true)  
  13. public class RoleServiceImpl implements RoleService {  
  14.   
  15.     private RoleDao rdao;  
  16.   
  17.     public void setRoleDao(RoleDao rdao) {  
  18.         this.rdao = rdao;  
  19.     }  
  20.       
  21.     @Transactional(readOnly = false, propagation = Propagation.REQUIRED)  
  22.     public void saveRole(Role r) {  
  23.         this.rdao.save(r);  
  24.     }  
  25.   
  26.     @Transactional()  
  27.     public List<role> findAll() {  </role>
  28.         List<role> l =  this.rdao.find();  </role>
  29.         return l;  
  30.     }  
  31.       
  32. }  
以及DAO的接口RoleDao:
java 代码
 
  1. package wanged.security.dao;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.hibernate.SessionFactory;  
  6.   
  7. import wanged.security.entity.Role;  
  8.   
  9. public interface RoleDao {  
  10.   
  11.     void setSessionFactory(SessionFactory sessionFactory);  
  12.   
  13.     void save(Role r);  
  14.   
  15.     List<role> find();  </role>
  16. }  
及其实现RoleDaoImpl:
java 代码
 
  1. package wanged.security.dao.impl;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.hibernate.SessionFactory;  
  6.   
  7. import wanged.security.dao.RoleDao;  
  8. import wanged.security.entity.Role;  
  9.   
  10. public class RoleDaoImpl implements RoleDao {  
  11.     private SessionFactory sessionFactory;  
  12.   
  13.     public void setSessionFactory(SessionFactory sessionFactory) {  
  14.         this.sessionFactory = sessionFactory;  
  15.     }  
  16.   
  17.     public void save(Role r) {  
  18.         sessionFactory.getCurrentSession().save(r);  
  19.     }  
  20.       
  21.     @SuppressWarnings("unchecked")  
  22.     public List<role> find(){  </role>
  23.         return sessionFactory.getCurrentSession().createQuery("from " + Role.class.getName()).list();  
  24.     }  
  25. }  
这些都是最常见的没什么可注意的,下面主要说说配置。
配置文件还是Bean初始化使用bean.xml:
xml 代码
 
  1. <!-- 数据持久化 -->  
  2. <bean id="roleDao" class="wanged.security.dao.impl.RoleDaoImpl">  
  3.   <property name="sessionFactory" ref="sessionFactory" />  
  4. </bean>  
  5.   
  6. <bean id="roleService" class="wanged.security.service.impl.RoleServiceImpl">  
  7.   <property name="roleDao" ref="roleDao" />  
  8. </bean>  
  9.   
  10. <!-- 事务处理 -->  
  11. <tx:annotation-driven transaction-manager="txManager" />  
  12.   
  13. <!--  Test  -->  
  14. <bean id="test" class="wanged.RoleServiceTest" init-method="init">  
  15.   <property name="roleService" ref="roleService" />  
  16. </bean>  

这里引用了上文中在"wanged_core_persistent" Bundle中 声明的服务sessionFactory和 txManager,这个 txManager是一个默认的事务管理方案,如果不合适可以在这个xml文件中自定义,这就为新扩展的数据库操作提供了方便。另外这里有一个测试类 RoleServiceTest,是用来测试这个Bundle是否能正常工作的,可以自己编写其中的代码。
服务的声明和引用定义在osgi-service.xml文件中:
xml 代码
 
  1. <osgi:reference id="sessionFactory" interface="org.hibernate.SessionFactory" />    
  2.   
  3. <osgi:reference interface="org.springframework.transaction.PlatformTransactionManager" id="txManager" />    
  4.   
  5. <osgi:service interface="wanged.security.service.RoleService" ref="roleService" />  
这里声明了一个 RoleService的服务供其它的Bundle使用。
       到目前为止,OSGi、Spring、Hibernate已经成功整合在一起。经运行测试,一切正常。但还没有对SessionFactory的重新初始化进行处理,不过这已经不是重点了。
       后续博客会将对Jetty和Wicket的整合陆续写出来,最终形成OSGi、Spring、Hibernate、Jetty和Wicket搭建起来的面向服务的基于组件的体系。
       这个周末所有的时间都放在Wicket和Jetty上面终于将其调试通过,写出来希望能给广大道友一点帮助,不至于像我刚开始那样盲目与不知所措。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值