By Alex
/ in
包含Spring、Hibernate、事务管理等功能的样例
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(); } }
ApplicationContext ctx = new AnnotationConfigApplicationContext(BeanDefinitionRegistrar.class); DataSource ds= ctx.getBean(DataSource.class);
Leave a Reply to 张秉清 Cancel reply