在命令行配置参数
使用jar包启动的时候,参数必须以 --
开头参数放在结尾,或者使用-D
开头,参数放在开始才符合jar参数规范
使用 mvn启动时,参数必须以 -D开头才符合mvn参数规范
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
创建一个普通的非执行的jar包(library)
移除pom文件中的spring-boot-maven-plugin
多数据源和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();
}
}
第二个数据源
@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
/config
subdirectory of the current directory. (即当前启动spring-boot程序文件夹的/config
路径) - The
current
directory(即当前启动spring-boot程序文件夹的./
路径) - A
classpath /config
package (当前jar/war包classpath
的/config
路径) - The
classpath
root (当前jar/war包classpath
的./
路径)
以上是优先级从大到小排序,高优先级的会覆盖低优先的值
其中,如果激活了spring.profiles.active=xx
,那么active
的properties的优先级是最高的,所以如果使用外部文件配置,最好不要激活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
的优先级是最高的
方法二
@SpringBootApplication
@PropertySources({
@PropertySource("classpath:application.properties"),
// 引用外部配置文件,不会覆盖已有属性,file开头的优先级最低
@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);
}
}
网友评论