SpringBoot中yml应用小技巧

记录一下开发过程中的一些小技巧

1.多环境配置
在这里插入图片描述
2.开发工具中使用不同环境的配置

#启动项目后使用application-dev.yml
spring: 
  profiles: 
    active: dev

3.项目端口设定

server:
  port: 8081

4.数据源配置

spring: 
  dataSource: 
    url: jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=xxxxxxxx)(PORT=xxxx))(CONNECT_DATA=(SERVICE_NAME=xxxx)))
    username: xxxx
    password: xxxx
    driver-class-name: oracle.jdbc.driver.OracleDriver

5.增加项目名配置

server: 
  servlet: 
    context-path: /patent

6.日志

logging:
  file: C:\robotap\patent\Log\xxx.log

7.多环境发布

java -jar xxxx.jar spring.profiles.active=dev

8.多数据源配置(数据源配置在tomcat中的情况)
在application.java的同一层级增加
DataSourceConfig.java
Datasource1.java
Datasource2.java

@Configuration
public class DataSourceConfig {

    @Bean(name = "primaryDataSource")
    @Qualifier("primaryDataSource")
    //@ConfigurationProperties(prefix = "spring.datasource.edi")
    public DataSource primaryDataSource() throws NamingException{
    	JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
        bean.setJndiName("java:comp/env/EPDS" );
        bean.setProxyInterface(DataSource.class);
        bean.setLookupOnStartup(false);
        bean.afterPropertiesSet();
        return (DataSource) bean.getObject();
    	//return DataSourceBuilder.create().build();
    }


    @Bean(name = "secondaryDataSource")
    @Qualifier("secondaryDataSource")
    @Primary
    //@ConfigurationProperties(prefix = "spring.datasource.eed")
    public DataSource secondaryDataSource() throws NamingException{
    	JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
        bean.setJndiName("java:comp/env/RPADS" );
        bean.setProxyInterface(DataSource.class);
        bean.setLookupOnStartup(false);
        bean.afterPropertiesSet();
        return (DataSource) bean.getObject();
    	//return DataSourceBuilder.create().build();
    }
    
}
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
		entityManagerFactoryRef = "entityManagerFactoryPrimary",//配置連接工廠 entityManagerFactory   
		transactionManagerRef = "transactionManagerPrimary",	//配置事務管理器  transactionManager   
		basePackages = {"com.mitac.rpa.dao.bean.epRepository"}	//設置持久層所在位置  
)
public class Datasource1{
    
	@Autowired
    @Qualifier("primaryDataSource")
    private DataSource primaryDataSource;
	
	@Autowired
	private JpaProperties jpaProperties;
	
	@Value("${spring.jpa.database-platform}")
	private String primaryDialect;
	
	@Bean(name = "entityManagerPrimary")
	public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
		return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
	}
	
	/**
	  *@param builder
	  *@return
	 */
	@Bean(name = "entityManagerFactoryPrimary")
	public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
		return builder
		//设置数据源
		.dataSource(primaryDataSource)
		//设置数据源属性
		.properties(getVendorProperties())
		//设置实体类所在位置.扫描所有带有 @Entity 注解的类           
		.packages("com.mitac.rpa.dao.bean")
		//Spring会将EntityManagerFactory注入到Repository之中,有了 EntityManagerFactory之后,           
		//Repository就能用它来创建 EntityManager了,然后 EntityManager就可以针对数据库执行操作           
		.persistenceUnit("primaryPersistenceUnit")
		.build();
	}
	
	private Map getVendorProperties() {
		Map map = new HashMap<>();
		map.put("hibernate.dialect",primaryDialect);// 设置对应的数据库方言 
		map.put("hibernate.hbm2ddl.auto","none");
		jpaProperties.setProperties(map);
		return jpaProperties.getProperties();
	}
	
	/**
	  * * 配置事物管理器 *
	  * * @param builder
	  * * @return
	  */
	@Bean(name = "transactionManagerPrimary")
	PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
		return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
	}
    
}
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
		entityManagerFactoryRef = "entityManagerFactorySecondary",//配置连接工厂 entityManagerFactory   
		transactionManagerRef = "transactionManagerSecondary",	//配置 事物管理器  transactionManager   
		basePackages = {"com.mitac.rpa.dao.bean.jpaRepository"}	//设置持久层所在位置  
)
public class Datasource2{
    
	@Autowired
    @Qualifier("secondaryDataSource")
    private DataSource secondaryDataSource;
	
	@Autowired
	private JpaProperties jpaProperties;
	
	@Value("${spring.jpa.database-platform}")
	private String primaryDialect;
	
	@Primary
	@Bean(name = "entityManagerSecondary")
	public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
	return entityManagerFactorySecondary(builder).getObject().createEntityManager();
	}
	
	/**
	  *@param builder
	  *@return
	 */
	@Primary
	@Bean(name = "entityManagerFactorySecondary")
	public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary(EntityManagerFactoryBuilder builder) {
		return builder
		//设置数据源
		.dataSource(secondaryDataSource)
		//设置数据源属性
		.properties(getVendorProperties())
		//设置实体类所在位置.扫描所有带有 @Entity 注解的类           
		.packages("com.mitac.rpa.dao.bean")
		//Spring会将EntityManagerFactory注入到Repository之中,有了 EntityManagerFactory之后,           
		//Repository就能用它来创建 EntityManager了,然后 EntityManager就可以针对数据库执行操作           
		.persistenceUnit("secondaryPersistenceUnit")
		.build();
	}
	
	private Map getVendorProperties() {
		Map map = new HashMap<>();
		map.put("hibernate.dialect",primaryDialect);// 设置对应的数据库方言 
		map.put("hibernate.hbm2ddl.auto","none");
		jpaProperties.setProperties(map);
		return jpaProperties.getProperties();
	}
    
	/**
	  * * 配置事物管理器 *
	  * * @param builder
	  * * @return
	  */
	@Primary
	@Bean(name = "transactionManagerSecondary")
	PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
		return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
	}
    
}

你可能感兴趣的:(spring,boot)