基于JavaConfig方式的Spring+Hibernate集成
JavaConfig类
包含Spring、Hibernate、事务管理等功能的样例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
package sparknet.wnet.saic.intf.spring; import java.io.IOException; import java.util.Properties; import javax.inject.Inject; import javax.sql.DataSource; import net.greenmemory.breeze.persist.OracleProxoolDataSource; import net.greenmemory.commons.db.QueryRunner; import net.greenmemory.commons.lang.NumberUtils; import net.greenmemory.spring.core.io.ClassPathResource; import org.hibernate.SessionFactory; import org.springframework.aop.framework.autoproxy.InfrastructureAdvisorAutoProxyCreator; import org.springframework.beans.factory.BeanInitializationException; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.context.annotation.FilterType; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.orm.hibernate3.HibernateTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource; import org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor; import org.springframework.transaction.interceptor.TransactionInterceptor; import cc.gmem.tools.hibernatetools.spring.Hibernate3LocalSessionFactoryBean; @Configuration @ComponentScan ( basePackages = { "cc.gmem.saic", "cc.gmem.intf" }, excludeFilters = { @Filter ( type = FilterType.ANNOTATION, value = Configuration.class ) } ) @EnableAspectJAutoProxy public class BeanDefinitionRegistrar { @Inject private ApplicationContext applicationContext; @Bean ( name = "cfg" ) public Properties getAppConfig() { Properties props = new Properties(); try { props.load( new ClassPathResource( "wsi.properties" ).getInputStream() ); } catch ( IOException e ) { throw new BeanInitializationException( e.getMessage(), e ); } return props; } @Bean ( name = "dataSource" ) public DataSource getDataSource() { OracleProxoolDataSource dataSource = new OracleProxoolDataSource(); dataSource.setAlias( getAppConfig().getProperty( "jdbc.alias" ) ); dataSource.setDriver( getAppConfig().getProperty( "jdbc.driverClass" ) ); dataSource.setDriverUrl( getAppConfig().getProperty( "jdbc.url" ) ); dataSource.setUser( getAppConfig().getProperty( "jdbc.user" ) ); dataSource.setPassword( getAppConfig().getProperty( "jdbc.password" ) ); dataSource.setMinimumConnectionCount( NumberUtils.toInt( getAppConfig().getProperty( "jdbc.minimumConnectionCount" ), 10 ) ); dataSource.setMaximumConnectionCount( NumberUtils.toInt( getAppConfig().getProperty( "jdbc.maximumConnectionCount" ), 100 ) ); dataSource.setSimultaneousBuildThrottle( NumberUtils.toInt( getAppConfig().getProperty( "jdbc.simultaneousBuildThrottle" ), 50 ) ); dataSource.setMaximumActiveTime( NumberUtils.toInt( getAppConfig().getProperty( "jdbc.maximumActiveTime" ), 1200000 ) ); dataSource.setEncrypted( false ); dataSource.init(); return dataSource; } @Bean ( name = "sessionFactory" ) public SessionFactory getSessionFactory() throws Exception { Hibernate3LocalSessionFactoryBean bean = new Hibernate3LocalSessionFactoryBean(); bean.setApplicationContext( applicationContext ); bean.setDataSource( getDataSource() ); bean.setMappingResources( new String[] { "wnetJhInterface.hbm.xml" } ); bean.setAutoRegister( true ); Properties hibernateProperties = new Properties(); hibernateProperties.setProperty( "hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect" ); hibernateProperties.setProperty( "hibernate.jdbc.batch_size", "100" ); hibernateProperties.setProperty( "hibernate.default_entity_mode", "dom4j" ); hibernateProperties.setProperty( "hibernate.transaction.factory_class", "org.hibernate.transaction.JDBCTransactionFactory" ); hibernateProperties.setProperty( "hibernate.current_session_context_class", "org.springframework.orm.hibernate3.SpringSessionContext" ); bean.setHibernateProperties( hibernateProperties ); bean.afterPropertiesSet(); return bean.getObject(); } @Bean ( name = "txManager" ) public PlatformTransactionManager getTransactionManager() throws Exception { HibernateTransactionManager txMgr = new HibernateTransactionManager(); txMgr.setSessionFactory( getSessionFactory() ); return txMgr; } @Bean ( name = "jdbcTemplate" ) public JdbcTemplate getJdbcTemplate() { return new JdbcTemplate( getDataSource() ); } @Bean ( name = "queryRunner" ) public QueryRunner getQueryRunner() { return new QueryRunner( getDataSource() ); } @Bean public AnnotationTransactionAttributeSource transactionAttributeSource() { return new AnnotationTransactionAttributeSource(); } @Bean public TransactionInterceptor transactionInterceptor() throws Exception { return new TransactionInterceptor( getTransactionManager(), transactionAttributeSource() ); } @Bean public TransactionAttributeSourceAdvisor internalTransactionAdvisor() throws Exception { return new TransactionAttributeSourceAdvisor( transactionInterceptor() ); } @Bean public InfrastructureAdvisorAutoProxyCreator internalAutoProxyCreator() { return new InfrastructureAdvisorAutoProxyCreator(); } } |
初始化Spring ApplicationContext的代码
1 2 |
ApplicationContext ctx = new AnnotationConfigApplicationContext(BeanDefinitionRegistrar.class); DataSource ds= ctx.getBean(DataSource.class); |
Leave a Reply