SpringApplication会默认将命令行选项参数转换为配置信息
-
通过maven命令启动:
1.1 mvn spring-boot:run
1.2 自定义启动
@ComponentScan(“com.sjzl”)
@EnableAutoConfiguration
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class,args);
}
} -
设置端口号
java -jar sell.jar --server.port=8090 -
设置启动环境
spring.profiles.active = dev
-Dspring.profiles.active=dev
java -jar sell.jar --spring.profiles.active = dev
java -jar -Dserver.port=8090 -Dspring.profiles.active=dev sell.jar -
禁用外部命令行
从命令行指定配置项的优先级最高,禁用如下:
SpringApplication.setAddCommandLineProperties(false) -
全局配置文件
除了已知的那两个,还有一个放在类路径的/config下
当前目录下的/config目录
当前目录
classpath里的/config目录
classpath 跟目录
因此,要外置配置文件就很简单了,在jar所在目录新建config文件夹,然后放入配置文件,或者直接放在配置文件在jar目录 -
自定义配置文件
java -jar sell.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
或者
java -jar -Dspring.config.location=D:\config\config.properties sell.jar
当然,还能在代码里指定
@SpringBootApplication
@PropertySource(“classpath:mock-data.properties”)
@ConfigurationProperties(prefix = “mock”)
public class sellApplication {
public static void main(String[] args) {
SpringApplication.run(sell.class, args);
}
} -
查看源码的三种方式
1.java -jar xx.jar - -debug
2.在properties中设置debug: true
3.idea设置参数-Ddebug -
查看自动配置
打开调试日志
在application.properties打开调试日志:
logging.level.org.springframework: DEBUG
当启动程序时,会打印自动配置日志信息