美文网首页
springtboot jndi 数据源配置

springtboot jndi 数据源配置

作者: SuMan | 来源:发表于2018-07-11 11:35 被阅读0次

    JPAConfig.java

    package com.ctop.nsb.config;
    
    import com.ctop.nsb.util.PropertiesUtil;
    import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
    import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;
    import org.springframework.orm.jpa.JpaTransactionManager;
    import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
    import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
    import org.springframework.transaction.PlatformTransactionManager;
    import org.springframework.transaction.annotation.EnableTransactionManagement;
    
    import javax.persistence.EntityManagerFactory;
    import javax.sql.DataSource;
    
    /**
     */
    @Configuration
    @EnableJpaRepositories(basePackages = "com.ctop.nsb.repository")
    @EnableTransactionManagement
    public class JPAConfig {
        @Bean
        @ConfigurationProperties(prefix = "spring.datasource")
        public DataSource dataSource() {
            if ("develop".equals(PropertiesUtil.getProperty("EnvironmentConfig"))) {
                return DataSourceBuilder.create().build();
            }
            JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
            return dataSourceLookup.getDataSource(PropertiesUtil.getProperty("dataSource.jndi"));
        }
    
        @Bean
        public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
            HibernateJpaVendorAdapter japVendor = new HibernateJpaVendorAdapter();
            japVendor.setGenerateDdl(false);
    
            LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
            entityManagerFactory.setDataSource(dataSource());
            entityManagerFactory.setJpaVendorAdapter(japVendor);
            entityManagerFactory.setPackagesToScan("com.ctop.nsb.entity");
            return entityManagerFactory;
        }
    
        @Bean
        public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
            JpaTransactionManager transactionManager = new JpaTransactionManager();
            transactionManager.setEntityManagerFactory(entityManagerFactory);
            return transactionManager;
        }
    }
    
    

    相关文章

      网友评论

          本文标题:springtboot jndi 数据源配置

          本文链接:https://www.haomeiwen.com/subject/znfhpftx.html