23.7访问应用程序参数
如果您需要访问传递给 SpringApplication.run(… ) 的应用程序参数,则可以注入 org.springframework.boot.ApplicationArguments
bean。ApplicationArguments 接口提供对原始 String[] 参数以及解析的 option 和 non-option 参数的访问,如以下示例所示:
import org.springframework.boot.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;
@Component
public class MyBean {
@Autowired
public MyBean(ApplicationArguments args) {
boolean debug = args.containsOption("debug");
List<String> files = args.getNonOptionArgs();
// if run with "--debug logfile.txt" debug=true, files=["logfile.txt"]
}
}
Spring Boot还注册 CommandLinePropertySource 和Spring Environment 。这使您还可以使用 @Value 注释注入单个应用程序
参数。
23.8使用ApplicationRunner或CommandLineRunner
如果您需要在 SpringApplication 启动后运行某些特定代码,则可以实现 ApplicationRunner 或 CommandLineRunner 接口。两个接口以相
同的方式工作,并提供单个 run 方法,该方法在 SpringApplication.run(… ) 完成之前调用。
CommandLineRunner 接口提供对应用程序参数的访问,作为简单的字符串数组,而 ApplicationRunner 使用前面讨论
的 ApplicationArguments 接口。以下示例显示 CommandLineRunner 和 run 方法:
import org.springframework.boot.*;
import org.springframework.stereotype.*;
@Component
public class MyBean implements CommandLineRunner {
public void run(String... args) {
// Do something...
}
}
如果定义了必须按特定顺序调用的多个 CommandLineRunner 或 ApplicationRunner beans,则可以另外实
现 org.springframework.core.Ordered 接口或使用 org.springframework.core.annotation.Order 注释。
23.9申请退出
每个 SpringApplication 都会向JVM注册一个关闭钩子,以确保 ApplicationContext 在退出时正常关闭。可以使用所有标准Spring生命周期
回调(例如 DisposableBean 接口或 @PreDestroy 注释)。
此外,beans如果希望在调用 SpringApplication.exit() 时返回特定的退出代码,则可以实
现 org.springframework.boot.ExitCodeGenerator 接口。然后可以将此退出代码传递给 System.exit() 以将其作为状态代码返回,如以下
示例所示:
@SpringBootApplication
public class ExitCodeApplication {
@Bean
@Bean
public ExitCodeGenerator exitCodeGenerator() {
return () -> 42;
}
public static void main(String[] args) {
System.exit(SpringApplication
.exit(SpringApplication.run(ExitCodeApplication.class, args)));
}
}
此外, ExitCodeGenerator 接口可以通过例外来实现。遇到这样的异常时,Spring Boot返回实现的 getExitCode() 方法提供的退出代码。
23.10管理员功能
通过指定 spring.application.admin.enabled 属性,可以为应用程序启用与管理相关的功能。这暴露了 SpringApplicationAdminMXBean
平台 MBeanServer 。您可以使用此功能远程管理您的Spring Boot应用程序。此功能对于任何服务包装器实现也很有用。
如果您想知道应用程序正在运行的HTTP端口,请使用 local.server.port 的密钥获取该属性。
警告
启用此功能时要小心,因为MBean公开了一种关闭应用程序的方法。
24.外部配置
Spring Boot允许您外部化您的配置,以便您可以在不同的环境中使用相同的应用程序代码。您可以使用属性文件,YAML文件,环境变量和命
令行参数来外部化配置。Property值可以通过使用 @Value 注释直接注入beans,通过Spring的 Environment 抽象访问,或 通
过 @ConfigurationProperties 绑定到结构化对象。
Spring Boot使用非常特殊的 PropertySource 顺序,旨在允许合理地覆盖值。按以下顺序考虑属性:
1. Devtools 主目录上的全局设置属性(当devtools处于活动状态时 ~/.spring-boot-devtools.properties )。
2. @TestPropertySource 测试上的注释。
3. properties 属于您的测试。可 用于测试特定应用程序片段 @SpringBootTest 的 测试注释。
4. 命令行参数。
5. 来自 SPRING_APPLICATION_JSON 的属性(嵌入在环境变量或系统属性中的内联JSON)。
6. ServletConfig init参数。
7. ServletContext init参数。
8. JNDI来自 java:comp/env 。
9. Java系统属性( System.getProperties() )。
10. OS环境变量。
11. RandomValuePropertySource 仅在 random.* 中具有属性。
12. 特定于配置文件的应用程序属性在打包的jar之外( application-{profile}.properties 和YAML变体)。
13. 打包在jar中的特定于配置文件的应用程序属性( application-{profile}.properties 和YAML变体)。
14. 打包jar之外的应用程序属性( application.properties 和YAML变体)。
15. 打包在jar中的应用程序属性( application.properties 和YAML变体)。
16. @PropertySource @Configuration 课程上的注释。
17. 默认属性(通过设置 SpringApplication.setDefaultProperties 指定)。
为了提供一个具体示例,假设您开发了一个使用 name 属性的 @Component ,如以下示例所示:
import org.springframework.stereotype.*;
import org.springframework.beans.factory.annotation.*;
@Component
public class MyBean {
@Value("${name}")
private String name;
// ...
}
在应用程序类路径上(例如,在jar中),您可以拥有一个 application.properties 文件,为 name 提供合理的默认属性值。在新环境中运行
时,可以在jar之外提供覆盖 name 的 application.properties 文件。对于一次性测试,您可以使用特定的命令行开关启动(例
如, java -jar app.jar --name="Spring" )。
可以在命令行上使用环境变量提供 SPRING_APPLICATION_JSON 属性。例如,您可以在UN * X shell中使用以下行:
$ SPRING_APPLICATION_JSON='{"acme":{"name":"test"}}' java -jar myapp.jar
在前面的示例中,您最终在Spring Environment 中使用 acme.name=test 。您还可以在System属性中将JSON提供
为 spring.application.json ,如以下示例所示:
$ java -Dspring.application.json='{"name":"test"}' -jar myapp.jar
您还可以使用命令行参数提供JSON,如以下示例所示:
$ java -jar myapp.jar --spring.application.json='{"name":"test"}'
您还可以将JSON作为JNDI变量提供,如下所示:java:comp/env/spring.application.json 。