环境:SpringBoot3.0.9
1. 访问命令行属性
默认情况下,SpringApplication会将任何命令行选项参数(即以--开头的参数,例如--server.port=9000)转换为属性,并将它们添加到SpringEnvironment中。如前所述,命令行属性总是优先于基于文件的属性源。如果你不希望命令行属性被添加到环境中,可以使用SpringApplication.setAddCommandLineProperties(false)禁用它们。访问命令行属性:
public class SpringbootComprehensiveApplication implements CommandLineRunner {@Value("${pack.name}")private String name ;@Value("${pack.age}")private Integer age ;public static void main(String[] args) {SpringApplication.run(SpringbootComprehensiveApplication.class, args);}@Overridepublic void run(String... args) throws Exception {System.out.println(name) ;System.out.println(age) ;}}

输出
张三66
你也可以下面这样访问
@Resourceprivate ApplicationArguments arguments ;// 访问System.out.println(arguments.getOptionValues("pack.name")) ;
2. JSON应用程序属性
环境变量和系统属性通常具有限制,这意味着不能使用某些属性名称。为了帮助实现这一点,SpringBoot允许将属性编码为单个JSON结构。当应用程序启动时,任何spring.application.json或spring_application_json属性都将被解析并添加到Environment中。
以系统属性方式添加:
-Dspring.application.json={\"user\":{\"name\":\"pack\"}}
访问方式与上面一直。
以命令行参数形式:
--spring.application.json={\"user\":{\"name\":\"pack-args\"}}
3. 外部应用程序属性
当应用程序启动时,Spring Boot将自动从以下位置查找和加载application.properties和application.yaml文件:
从classpath
1) classpath根目录。
2)classpath下的/config包中。从当前目录
1)当前目录
2)当前目录下的/config目录3)config/子目录的直接子目录
该列表按优先级排序(较低项的值覆盖较早项的值)。加载文件中的文档作为PropertySources添加到Spring Environment中。
修改配置文件的名称
java -jar app.jar --spring.config.name=pack
指定配置文件位置
java -jar app.jar --spring.config.location=\optional:classpath:/pack.properties,\optional:classpath:/other.properties
注:使用前缀optional:如果位置是可选的,也就是这些文件可以不存在。spring.config.name、spring.config.location和spring.onfig.additional-location在很早就被用来确定哪些文件需要加载。它们必须定义为环境属性(通常是操作系统环境变量、系统属性或命令行参数)。
如果spring.config.location包含目录(而不是文件),它们应该以/结尾。在运行时,它们将在加载之前附加由spring.config.name生成的名称。spring.config.location中指定的文件将直接导入。
4. 加密属性
SpringBoot不提供对加密属性值的任何内置支持,但它确实提供了修改Spring环境中包含的值所需的钩子点。EnvironmentPostProcessor接口允许您在应用程序启动之前操纵环境。
请查看《Springboot中的不使用第三方插件对敏感信息加密处理,这种方式你知道吗?》
5. 直接加载YAML
SpringFramework提供了两个方便的类,可以用于加载YAML文档。YamlPropertiesFactoryBean将YAML作为Properties加载,YamlMapFactoryBean将YAML作为Map加载。
如果希望将YAML作为Spring PropertySource加载,也可以使用YamlPropertyResourceLoader类。示例:
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean() ;factory.setResources(new ClassPathResource("com/pack/yml/loader/pack.yml"));System.out.println(factory.getObject()) ;------------------------------------------------YamlMapFactoryBean factory = new YamlMapFactoryBean() ;factory.setResources(new ClassPathResource("com/pack/yml/loader/pack.yml")) ;
6. 配置随机值
RandomValuePropertySource用于注入随机值(例如,在秘密或测试用例中)。它可以生成整数、long、uuid或字符串,如下面的例子所示:
pack:secret: "${random.value}"sno: "${random.int}"longnum: "${random.long}"uuid: "${random.uuid}"number-less-than-ten: "${random.int(10)}"number-in-range: "${random.int[1024,65536]}"
random.int*语法是OPEN value(,max)CLOSE,其中OPEN、CLOSE是任何字符,value、max是整数。如果提供了max,则value是最小值,max是最大值(不包括)。
7. 国际化
SpringBoot支持本地化消息,因此应用程序可以满足不同语言的展示。默认情况下,SpringBoot在类路径的根处查找消息资源。
可以使用spring.messages命名空间配置资源的基本名称以及几个其他属性,如下例所示:
spring:messages:: message: false
如下图:

资源属性访问
private MessageSourceAccessor accessor ;("/index")public String index() {return accessor.getMessage("user.age.range", new Object[] {1, 100}) ;}
8. 访问Banner及自定义
// springboot启动时会将Banner对象注册为Bean@Resourceprivate Banner springBootBanner ;public void run(String... args) throws Exception {springBootBanner.printBanner(null, null, System.out);}
在banner.txt文件中,可以使用环境中可用的任何键以及以下任何占位符:
| 变量 |
说明 |
| ${application.version} | 应用的版本号,在MANIFEST.MF中声明。例如,Implementation-Version: 1.0会打印为1.0。 |
| ${application.formatted-version} | 在MANIFEST中声明的应用的版本号。MF和格式化显示(括在括号中,以v开头)。例如(v1.0)。 |
| ${spring-boot.version} | 你正在使用的Spring Boot版本。例如3.0.9。 |
| ${spring-boot.formatted-version} | 你使用的Spring Boot版本号,格式化以便显示(用括号括起来,以v开头),例如v3.0.9。 |
${Ansi.NAME} (or ${AnsiColor.NAME}, ${AnsiBackground.NAME}, ${AnsiStyle.NAME}) |
其中NAME是ANSI转义码的名称。 |
| ${application.title} | 应用的标题,在MANIFEST.MF中声明。例如,Implementation-Title: MyApp会打印为MyApp。 |
9. Spring上下文(Web环境)
SpringApplication会自动识别创建正确类型的ApplicationContext。用于确定WebApplicationType的算法如下:
如果存在Spring MVC,则使用一个AnnotationConfigServletWebServerApplicationContext
如果Spring MVC不存在而Spring WebFlux存在,则会使用configreactivewebserverapplicationcontext注解
否则,使用AnnotationConfigApplicationContext
这意味着如果你在同一个应用程序中同时使用Spring MVC和Spring WebFlux的新WebClient, Spring MVC会被默认使用。你可以通过调用setWebApplicationType(WebApplicationType)轻松地覆盖它。也可以通过调用setApplicationContextFactory(…)来完全控制ApplicationContext类型。
new SpringApplication(SpringbootComprehensiveApplication.class).setWebApplicationType(WebApplicationType.SERVLET) ;
完毕!!!



