接上文,为了测试已经搭好的架子,创建了一个注册实体的Bundle(wanged_security_entity),这个里面包含了两个实体类(Role、User)和它们的Hibernate映射文件(Role.hbm.xml、User.hbm.xml),以及一个实现了EntityRegister接口的类(SecurityEntityRegisterImpl)以提供注册实体的服务。这里仅将SecurityEntityRegisterImpl的代码列出如下:
java 代码
package wanged.security.entity;
import java.util.ArrayList;
import wanged.core.persistent.entity.EntityRegister;
@SuppressWarnings("unchecked")
public class SecurityEntityRegisterImpl implements EntityRegister{
public Class[] register() {
ArrayList<class> cList = new ArrayList<class>(); </class></class>
cList.add(Role.class);
cList.add(User.class);
return cList.toArray(new Class[cList.size()]);
}
}
下面来声明该服务:
xml 代码
<osgi:service interface="wanged.core.persistent.entity.EntityRegister">
<bean class="wanged.security.entity.SecurityEntityRegisterImpl" />
</osgi:service>
这个服务就是上篇中,"wanged_core_persistent" Bundle中引用的服务,完成了实体类的注册。
现在有了实体类,就需要有对实体类进行CURD操作的Bundle(wanged_security_service),这个Bundle中采用了常见的Service-DAO模式,以对Role的操作为例,包括RoleService:
java 代码
package wanged.security.service;
import java.util.List;
import wanged.security.dao.RoleDao;
import wanged.security.entity.Role;
public interface RoleService {
void setRoleDao(RoleDao rdao);
void saveRole(Role r);
List<role> findAll(); </role>
}
和其实现RoleServiceImpl:
java 代码
package wanged.security.service.impl;
import java.util.List;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import wanged.security.dao.RoleDao;
import wanged.security.entity.Role;
import wanged.security.service.RoleService;
@Transactional(readOnly = true)
public class RoleServiceImpl implements RoleService {
private RoleDao rdao;
public void setRoleDao(RoleDao rdao) {
this.rdao = rdao;
}
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
public void saveRole(Role r) {
this.rdao.save(r);
}
@Transactional()
public List<role> findAll() { </role>
List<role> l = this.rdao.find(); </role>
return l;
}
}
以及DAO的接口RoleDao:
java 代码
package wanged.security.dao;
import java.util.List;
import org.hibernate.SessionFactory;
import wanged.security.entity.Role;
public interface RoleDao {
void setSessionFactory(SessionFactory sessionFactory);
void save(Role r);
List<role> find(); </role>
}
及其实现RoleDaoImpl:
java 代码
package wanged.security.dao.impl;
import java.util.List;
import org.hibernate.SessionFactory;
import wanged.security.dao.RoleDao;
import wanged.security.entity.Role;
public class RoleDaoImpl implements RoleDao {
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public void save(Role r) {
sessionFactory.getCurrentSession().save(r);
}
@SuppressWarnings("unchecked")
public List<role> find(){ </role>
return sessionFactory.getCurrentSession().createQuery("from " + Role.class.getName()).list();
}
}
这些都是最常见的没什么可注意的,下面主要说说配置。
配置文件还是Bean初始化使用bean.xml:
xml 代码
<!-- 数据持久化 -->
<bean id="roleDao" class="wanged.security.dao.impl.RoleDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="roleService" class="wanged.security.service.impl.RoleServiceImpl">
<property name="roleDao" ref="roleDao" />
</bean>
<!-- 事务处理 -->
<tx:annotation-driven transaction-manager="txManager" />
<!-- Test -->
<bean id="test" class="wanged.RoleServiceTest" init-method="init">
<property name="roleService" ref="roleService" />
</bean>
这里引用了上文中在"wanged_core_persistent" Bundle中声明的服务sessionFactory和txManager,这个txManager是一个默认的事务管理方案,如果不合适可以在这个xml文件中自定义,这就为新扩展的数据库操作提供了方便。另外这里有一个测试类RoleServiceTest,是用来测试这个Bundle是否能正常工作的,可以自己编写其中的代码。
服务的声明和引用定义在osgi-service.xml文件中:
xml 代码
<osgi:reference id="sessionFactory" interface="org.hibernate.SessionFactory" />
<osgi:reference interface="org.springframework.transaction.PlatformTransactionManager" id="txManager" />
<osgi:service interface="wanged.security.service.RoleService" ref="roleService" />
这里声明了一个RoleService的服务供其它的Bundle使用。
到目前为止,OSGi、Spring、Hibernate已经成功整合在一起。经运行测试,一切正常。但还没有对SessionFactory的重新初始化进行处理,不过这已经不是重点了。