您的位置:首页 > 移动开发

hibernate3-maven-plugin 使用 spring applicationContext.xml中的sessionFactory配置(2)

2010-08-15 12:12 489 查看
上面碰到的问题,我只能在网上找答案,完全照抄了这个链接的文章来试我的项目

 

http://mactruecolor.blogspot.com/2009/10/hibernate3-maven-pluginspring.html

 

不过这个链接经常访问不了,可能是被墙了吧。

 

我还参考了: http://techkriti.wordpress.com/2007/06/28/maven2-hibernate-plugin-and-spring/ 里面提到的maven和plexus 的关系,对理解这个改造过程很有用

 

 

把这个过程在这里再记一次下来。

 

在hibernate3这个插件里面,有一AnnotationComponentConfiguration,我们继承一下它,来读取sessionFactory那个bean

 

1.新建一个 maven项目

 

             该项目是为了生成一个Component(maven的是用plexus的component来做IOC注入的), 这个component(叫部件?)用来注入到hibernate3中以读取applicationContext.xml

 

 

1.1 pom.xml 如下

 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>cn.fshk</groupId>
<artifactId>hibernate</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.codehaus.mojo.hibernate3</groupId>
<artifactId>maven-hibernate3-jdk15</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.3.2.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.4.0.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>ejb3-persistence</artifactId>
<version>1.0.2.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.4.0.GA</version>
</dependency>
<dependency>
<groupId>jboss</groupId>
<artifactId>jboss-common</artifactId>
<version>4.0.2</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.4.GA</version>
</dependency>
<!--可視情形改用其他版本的Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-plugin-testing-harness</artifactId>
<version>1.1</version>
<type>jar</type>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project> 

 

1.3 项目中创建一个SpringComponentConfiguration类, 这个类改写了AnnotationComponentConfiguration的createConfiguration()方法,就在里面去读applicationContext.xml的sessionFactory那个bean

 

package cn.fshk.hibernate;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.mojo.hibernate3.ExporterMojo;
import org.codehaus.mojo.hibernate3.configuration.AnnotationComponentConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;
import org.springframework.util.StringUtils;
public class SpringComponentConfiguration extends
AnnotationComponentConfiguration {
private static final Log logger = LogFactory
.getLog(SpringComponentConfiguration.class);
private ExporterMojo exporterMojo;
private ApplicationContext applicationContext;

//这里与 /src/main/resources/plexus/component.xml中的rolehint节点值对应
public String getName() {
return "springconfiguration";
}
public ExporterMojo getExporterMojo() {
return exporterMojo;
}
public void setExporterMojo(ExporterMojo exporterMojo) {
this.exporterMojo = exporterMojo;
}
@Override
protected Configuration createConfiguration() {
String sessionFactoryBean = getExporterMojo().getComponentProperty(
"sessionFactoryBean", "sessionFactory");
String appContextLocations = getExporterMojo().getComponentProperty(
"appContextLocations", "classpath*:spring*.xml");

logger.info("Initial info: [sessionFactoryBean]=" + sessionFactoryBean
+ ", [appContextLocations]=" + appContextLocations);
String[] locations = StringUtils.delimitedListToStringArray(
appContextLocations, ",");

// Initial ApplicationContext from spring configuration xml files.
this.applicationContext = new FileSystemXmlApplicationContext(locations);

// Get AnnotationSessionFactoryBean from spring
AnnotationSessionFactoryBean asfb = (AnnotationSessionFactoryBean) applicationContext
.getBean("&" + sessionFactoryBean);

DataSource dataSource = asfb.getDataSource();
ThreadLocalHolder.setDataSource(dataSource);
Configuration configuration = asfb.getConfiguration();
configuration.setProperty(Environment.CONNECTION_PROVIDER,
ThreadLocalConnectionProvider.class.getName());

return configuration;
}
}
 

 

1.4 SpringComponentConfiguration用到了ThreadLocalConnectionProvider是管理datasource的,代码如下

 

package cn.fshk.hibernate;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.connection.ConnectionProvider;
public class ThreadLocalConnectionProvider implements ConnectionProvider {
private static final Log logger = LogFactory.getLog(ThreadLocalConnectionProvider.class);

public ThreadLocalConnectionProvider() {
}

public void close() throws HibernateException {
}

public void closeConnection(Connection conn) throws SQLException {
conn.close();
}

public void configure(Properties props) throws HibernateException {
//Do nothing
}

public Connection getConnection() throws SQLException {
DataSource dataSource = ThreadLocalHolder.getDataSource();
if (null == dataSource) {
logger.error("Please check ThreadLocalHolder.setDataSource has been invocked.");
throw new SQLException("No usable DataSource.");
}
return dataSource.getConnection();
}

public boolean supportsAggressiveRelease() {
// TODO Auto-generated method stub
return true;
}
}
 

 

1.5 ThreadLocalHolder类是上面用到的

 

package cn.fshk.hibernate;
import javax.sql.DataSource;
public abstract class ThreadLocalHolder {
private static ThreadLocal<DataSource> dataSourceHolder = new ThreadLocal<DataSource>();
public static DataSource getDataSource() {
return dataSourceHolder.get();
}
public static void setDataSource(DataSource dataSource) {
dataSourceHolder.set(dataSource);
}
}
 

 

 

1.6 创建项目的src/main/resources/META-INF/plexus/components.xml文件如下

 

     **注意<role-hine>与1.3步那个类的getName返回值相同

 

<?xml version="1.0" encoding="UTF-8"?>
<component-set>
<components>
<component>
<role>org.codehaus.mojo.hibernate3.configuration.ComponentConfiguration
</role>
<role-hint>springconfiguration</role-hint>
<implementation>cn.fshk.hibernate.SpringComponentConfiguration
</implementation>
</component>
</components>
</component-set>
 

 

1.7 鼠标右键项目, runas / maven install将这个component安装到本地库。

 

这样这个component就完成了。

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐