接上文,为了测试已经搭好的架子,创建了一个注册实体的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的重新初始化进行处理,不过这已经不是重点了。
分享到:
相关推荐
平台依赖度低的解决方案 ...测试环境:equinox3.5.2。其它用到的Bundle包括hibernate、hibernate-annotation、hsqldb、 spring、spring-osgi等。请到http://www.springsource.com/repository/下载。
辛苦研究了一个多月,终于搭建成了一个可用于实际应用开发的OSGI-WEB框架,采用OSGi+SpringMVC+Spring+Hibernate+Virgo技术,鉴于此类技术架构甚少,特提供出来供大家参考。 使用步骤: 一、下载STS搭建Osgi开发...
OSGI&spring;&hibernate;
《Eclipse RCP与Spring OSGi:技术详解与最佳实践》由资源的Eclipse专家亲自...实战篇(第13-15章)详细讲解了Eclipse RCP与Spring OSGi框架、Hibernate ORM框架、JPA规范、Maven工具的整合,以及它与Java的模块化设计
这是整合SpringMVC+Spring+SpringDataJPA+Hibernate简单的实现登录的功能,用的是mysql数据库,这是一个web Project 如果你用的是JavaEE6那么你要注意bean-validator.jar和weld-osgi-bundle.jar与slf4j的jar包冲突。...
将基于Hibernate 4和Spring 4的旧代码迁移到OSGi友好版本 当我听说OSGi Enterprise规范方法以及Spring团队使用其Spring DM服务器进入OSGi世界时,我真的以为OSGi很快将成为Java企业领域的标准。 但是随后,...
OSGi进阶源代码,OSGi集成webwork + spring + hibernate
osgi的基本概念 基于 osgi的实例 和spring的集成 和hibernate的集成
framework which includes Struts, Spring, Hibernate, and build an enterprise applications micro-core architecture called OSSH. Java programmers who are familiar with SSH can experience the benefits of ...
NULL 博文链接:https://georgezeng.iteye.com/blog/1533114
9) spring -hibernate.jar需spring-core.jar,spring-beans.jar,spring-aop.jar,spring- dao.jar,spring-jdbc.jar,spring-orm.jar,spring-web.jar,spring-webmvc.jar 10) spring-orm.jar需spring-core.jar,...
virgo+hibernate+spring+springmvc集成框架模板
spring 3.0已经全面支持OSGi了。 各发行包的大致描述如下: org.springframework.asm-3.0.0.M4.jar: 提供对ASM(一个字节码框架)的简单封装 org.springframework.expression-3.0.0.M4.jar: spring表达式语言 ...
* Building modular services using OSGi with Spring DM and Spring Dynamic Modules and SpringSource dm Server. * Delivering web applications with Spring Web Flow, Spring MVC, Spring Portals, Struts, ...
第1章 Spring简介 1 1.1 实例化Spring IoC容器 1 1.1.1 问题 1 1.1.2 解决方案 1 1.1.3 工作原理 3 1.2 配置Spring IoC容器中的Bean 4 1.2.1 问题 4 1.2.2 解决方案 4 1.2.3 工作原理 4 1.3 调用...
org.hibernate org.hsqldb org.jboss.javassist org.jboss.util 7号包: org.eclipse.persistence org.eclipse.jdt 8号包: org.osgi org.slf4j org.springframework org.springframework.build org.testng org....
第1章 Spring简介 1 1.1 实例化Spring IoC容器 1 1.1.1 问题 1 1.1.2 解决方案 1 1.1.3 工作原理 3 1.2 配置Spring IoC容器中的Bean 4 1.2.1 问题 4 1.2.2 解决方案 4 1.2.3 工作原理 4 1.3 调用...
在Java中整合SSH(Spring + Struts2 + Hibernate)框架时,需要引入相应的jar包。以下是一些基本的jar包和它们的用途: Spring基本和IOC的jar包: org.springframework.web-x.x.x.RELEASE.jar:在web.xml中配置...
spring 平台例子,提供了项目中基本你能想到的例子应用, email、jmx、ejb、aop、事务、mvc、spring mvc、authentication、jbpm、flex、rest、osgi、springbatch、web_flow、portletmvc
org.hibernate org.hsqldb org.jboss.javassist org.jboss.util 7号包: org.eclipse.persistence org.eclipse.jdt 8号包: org.osgi org.slf4j org.springframework org.springframework.build org.testng org....