先来说说整合Hibernate的关键之处。其实用OSGi整合Hibernate很简单,但要通过Bundle方式做到可以扩展新的持久化层面的东西(比如添加新的表和操作)就比较费事了。因为Hibernate在初始化时根据注册的实体类创建SessionFactory,这样当有新的实体类添加进来时就要创建新的SessionFactory,这样系统中出现两个甚至多个SessionFatory会导致一系列的问题。显然整合Hibernate关键就是解决实体类注册与SessionFactory创建的问题。
我的具体思路如下。
首先将Hibernate单独多为一个Bundle(wanged_commons_hibernate)以便提供其他Bundle所需类包。
然后建立一个用于提供实体注册接口的Bundle(wanged_core_persistent_entity_register),代码如下:
java 代码
package wanged.core.persistent.entity;
@SuppressWarnings("unchecked")
public interface EntityRegister {
/**
* 注册Hibernate的实体Class
* @return
*/
Class[] register();
}
建一个用来初始化SessionFactory和事务管理的Bundle(wanged_core_persistent),由于使用Spring提供的LocalSessionFactoryBean会有问题,所以我单独写了两个类。
类LocalSessionFactoryBean的代码:
java 代码
package wanged.core.persistent;
import java.util.HashMap;
import java.util.Properties;
import javax.sql.DataSource;
import org.hibernate.ConnectionReleaseMode;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
import org.springframework.orm.hibernate3.AbstractSessionFactoryBean;
import org.springframework.orm.hibernate3.SpringSessionContext;
import org.springframework.orm.hibernate3.TransactionAwareDataSourceConnectionProvider;
import wanged.core.persistent.entity.EntityRegister;
@SuppressWarnings("unchecked")
public class LocalSessionFactoryBean extends AbstractSessionFactoryBean {
private static final ThreadLocal configTimeDataSourceHolder = new ThreadLocal();
private Properties hibernateProperties;
private HashMapnew HashMap
private Configuration configuration;
public static DataSource getConfigTimeDataSource() {
return (DataSource) configTimeDataSourceHolder.get();
}
/**
* 注册Entity的Class数组
*
* @param er
*/
@SuppressWarnings("unchecked")
public void setEntityRegister(EntityRegister[] erArr) {
for (EntityRegister er : erArr) {
this.addEntityRegister(er);
}
}
@SuppressWarnings("unchecked")
public void setEntityRegister(EntityRegister er) {
this.addEntityRegister(er);
}
private void addEntityRegister(EntityRegister er){
// TODO:对registerClass()中取得的数组进行验证
this.entityClasses.put(er, er.register());
}
/**
* 卸载Entity的Class数组
*
* @param er
*/
public void unsetEntityRegister(EntityRegister er) {
this.entityClasses.remove(er);
// TODO:重新初始化SessionFactory
}
public void setHibernateProperties(Properties hibernateProperties) {
this.hibernateProperties = hibernateProperties;
}
public Properties getHibernateProperties() {
if (this.hibernateProperties == null) {
this.hibernateProperties = new Properties();
}
return this.hibernateProperties;
}
@SuppressWarnings("unchecked")
protected SessionFactory buildSessionFactory() {
Configuration config = new Configuration();
DataSource dataSource = getDataSource();
if (dataSource != null) {
configTimeDataSourceHolder.set(dataSource);
}
try {
config.setProperty(Environment.RELEASE_CONNECTIONS, ConnectionReleaseMode.ON_CLOSE.toString());
if (isExposeTransactionAwareSessionFactory()) {
config.setProperty(Environment.CURRENT_SESSION_CONTEXT_CLASS, SpringSessionContext.class.getName());
}
if (this.hibernateProperties != null) {
config.addProperties(this.hibernateProperties);
}
if (dataSource != null) {
boolean actuallyTransactionAware = (isUseTransactionAwareDataSource() || dataSource instanceof TransactionAwareDataSourceProxy);
config.setProperty(Environment.CONNECTION_PROVIDER, actuallyTransactionAware ? TransactionAwareDataSourceConnectionProvider.class
.getName() : LocalDataSourceConnectionProvider.class.getName());
}
// 添加Entity的类
for (Class[] cArr : this.entityClasses.values()) {
for (Class c : cArr) {
config.addClass(c);
}
}
this.configuration = config;
return config.buildSessionFactory();
} finally {
if (dataSource != null) {
configTimeDataSourceHolder.set(null);
}
}
}
/**
* Return the Configuration object used to build the SessionFactory. Allows
* access to configuration metadata stored there (rarely needed).
*
* @throws IllegalStateException
* if the Configuration object has not been initialized yet
*/
public final Configuration getConfiguration() {
if (this.configuration == null) {
throw new IllegalStateException("Configuration not initialized yet");
}
return this.configuration;
}
public void destroy() throws HibernateException {
DataSource dataSource = getDataSource();
if (dataSource != null) {
configTimeDataSourceHolder.set(dataSource);
}
try {
super.destroy();
} finally {
if (dataSource != null) {
configTimeDataSourceHolder.set(null);
}
}
}
}
类LocalDataSourceConnectionProvider的代码:
java 代码
package wanged.core.persistent;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import org.hibernate.HibernateException;
import org.hibernate.connection.ConnectionProvider;
import org.hibernate.util.JDBCExceptionReporter;
public class LocalDataSourceConnectionProvider implements ConnectionProvider {
private DataSource dataSource;
private DataSource dataSourceToUse;
public void configure(Properties props) throws HibernateException {
this.dataSource = LocalSessionFactoryBean.getConfigTimeDataSource();
if (this.dataSource == null) {
throw new HibernateException("No local DataSource found for configuration - " +
"dataSource property must be set on LocalSessionFactoryBean");
}
this.dataSourceToUse = getDataSourceToUse(this.dataSource);
}
protected DataSource getDataSourceToUse(DataSource originalDataSource) {
return originalDataSource;
}
public DataSource getDataSource() {
return dataSource;
}
public Connection getConnection() throws SQLException {
try {
return this.dataSourceToUse.getConnection();
}
catch (SQLException ex) {
JDBCExceptionReporter.logExceptions(ex);
throw ex;
}
}
public void closeConnection(Connection con) throws SQLException {
try {
con.close();
}
catch (SQLException ex) {
JDBCExceptionReporter.logExceptions(ex);
throw ex;
}
}
public void close() {
}
public boolean supportsAggressiveRelease() {
return false;
}
}
如果你对比其与Spring提供的同名类中的代码,相差不大。
下面来看看配置文件,我把Bean的初始化放在bean.xml中:
xml 代码
<!-- 本地调试使用连接池-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.gjt.mm.mysql.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/cms" />
<property name="username" value="root" />
<property name="password" value="root" />
<property name="connectionProperties">
<props>
<prop key="useUnicode">true</prop>
<prop key="characterEncoding">GBK</prop>
</props>
</property>
</bean>
<!-- 服务实现类定义 -->
<bean id="sessionFactory" class="wanged.core.persistent.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
<property name="dataSource" ref="dataSource" />
<property name="entityRegister" ref="entityRegister" />
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
而服务与引用的声明放在osgi-service.xml中:
xml 代码
<osgi:reference id="entityRegister" interface="wanged.core.persistent.entity.EntityRegister" cardinality="1..n"/>
<osgi:service interface="org.hibernate.SessionFactory" ref="sessionFactory" />
<osgi:service interface="org.springframework.transaction.PlatformTransactionManager" ref="txManager" />
这里的引用 "实体注册" 服务需要在我们的实体Bundle中实现,现在已经搭好了架子,下一步就需要创建自己的实体Bundle和数据库操作的Bundle。由于这个Blog保存时经常保存不住,需要重写,所以我尽可能的将其划分开来写。
分享到:
相关推荐
平台依赖度低的解决方案 ...测试环境: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 调用...
spring 平台例子,提供了项目中基本你能想到的例子应用, email、jmx、ejb、aop、事务、mvc、spring mvc、authentication、jbpm、flex、rest、osgi、springbatch、web_flow、portletmvc
在Java中整合SSH(Spring + Struts2 + Hibernate)框架时,需要引入相应的jar包。以下是一些基本的jar包和它们的用途: Spring基本和IOC的jar包: org.springframework.web-x.x.x.RELEASE.jar:在web.xml中配置...
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....