在命令行配置參數(shù)
使用jar包啟動的時候,參數(shù)必須以 -- 開頭參數(shù)放在結(jié)尾,或者使用-D開頭,參數(shù)放在開始才符合jar參數(shù)規(guī)范
使用 mvn啟動時,參數(shù)必須以 -D開頭才符合mvn參數(shù)規(guī)范
java -jar target/demo.jar --spring.profiles.active=dev
# 或者
java -Dspring.profiles.active=dev -jar target/demo.jar
mvn spring-boot:run -Dspring.profiles.active=dev
創(chuàng)建一個普通的非執(zhí)行的jar包(library)
移除pom文件中的spring-boot-maven-plugin
多數(shù)據(jù)源和Mybatis配置
java例子
@Configuration
// sqlSessionFactoryRef必填,否則使用@Primary注解的SqlSessionFactory
@MapperScan(value = "com.demo.app.mapper",
sqlSessionFactoryRef = Demo1Config.SQL_SESSION_FACTORY)
@EnableTransactionManagement(proxyTargetClass = true)
public class Demo1Config {
public static final String SQL_SESSION_FACTORY = "mybatisSqlSessionFactoryApp";
// ac-adaptor
@Bean
@ConfigurationProperties("spring.datasource.demo1")
public DataSource dataSourceApp() {
DataSource dataSource = DruidDataSourceBuilder.create().build();
return dataSource;
}
@Bean
public DataSourceTransactionManager txManagerApp() {
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
dataSourceTransactionManager.setDataSource(dataSourceApp());
return dataSourceTransactionManager;
}
@Bean(name = AcAppAdaptorConfig.SQL_SESSION_FACTORY)
public SqlSessionFactory mybatisSqlSessionFactoryApp() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSourceApp());
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(
"classpath*:config/mapper/com/demo/app/mapper/*.xml"));
return sessionFactory.getObject();
}
}
第二個數(shù)據(jù)源
@Configuration
@MapperScan(value = "com.petkit.ac.adaptor.chain.mapper",
sqlSessionFactoryRef = AcChainAdaptorConfig.SQL_SESSION_FACTORY)
@EnableTransactionManagement(proxyTargetClass = true)
public class AcChainAdaptorConfig {
public static final String SQL_SESSION_FACTORY = "mybatisSqlSessionFactoryChain";
// ac-adaptor
@Bean(name = AcChainAdaptorConfig.SQL_SESSION_FACTORY)
public SqlSessionFactory mybatisSqlSessionFactoryChain(@Autowired DataSource dataSource)
throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(
"classpath*:config/mapper/com/petkit/ac/adaptor/chain/mapper/*.xml"));
return sessionFactory.getObject();
}
}
打包war包以部署在tomcat下
pom配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
入口類
@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DemoApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
讀取jar包外部配置文件
Spring-boot在啟動時,如果不指定spring.config.location,將會從下面路徑中依次加載application.properties中的屬性:
- A
/configsubdirectory of the current directory. (即當(dāng)前啟動spring-boot程序文件夾的/config路徑) - The
currentdirectory(即當(dāng)前啟動spring-boot程序文件夾的./路徑) - A
classpath /configpackage (當(dāng)前jar/war包classpath的/config路徑) - The
classpathroot (當(dāng)前jar/war包classpath的./路徑)
以上是優(yōu)先級從大到小排序,高優(yōu)先級的會覆蓋低優(yōu)先的值
其中,如果激活了spring.profiles.active=xx,那么active的properties的優(yōu)先級是最高的,所以如果使用外部文件配置,最好不要激活profiles
方法一
java -jar demo.jar --spring.config.location=/opt/dev/demo.properties
或者
java -Dspring.config.location=/opt/dev/demo.properties -jar demo.jar
此時/opt/dev/demo.properties的優(yōu)先級是最高的
方法二
@SpringBootApplication
@PropertySources({
@PropertySource("classpath:application.properties"),
// 引用外部配置文件,不會覆蓋已有屬性,file開頭的優(yōu)先級最低
@PropertySource(value="file:${external.config}", ignoreResourceNotFound=true)
})
public class ChainBootApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(ChainBootApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(ChainBootApplication.class, args);
}
}