Property values can be injected directly into your beans using the @Value annotation, accessed via Spring’s Environment abstraction or bound to structured objects via @ConfigurationProperties .
参考文档 Externalized Configuration
SpringBoot 使用(三): 配置文件详解
Spring Boot 属性配置和使用
Spring Boot属性配置文件详解
demo 目录结构
公共配置文件 application.yml
方式一:@Value
组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 package com.johuer.like.demo;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;@Component public class ValueProperties { @Value("${my.name}") private String name; public String getName () { return name; } public void setName (String name) { this .name = name; } }
测试
ValuePropertiesTest
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 package com.johuer.like.demo;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import static org.junit.Assert.assertEquals;@RunWith(SpringRunner.class) @SpringBootTest public class ValuePropertiesTest { @Autowired private ValueProperties valueProperties; @Test public void testValueProperties () { assertEquals("johuer" , valueProperties.getName()); } }
方式二:@ConfigurationProperties
此方式有两种使用方式
使用方式1 组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 package com.johuer.like.demo;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;@Component @ConfigurationProperties(prefix = "my") public class ConfigProperties { private String name; public String getName () { return name; } public void setName (String name) { this .name = name; } }
测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 package com.johuer.like.demo;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import static org.junit.Assert.assertEquals;@RunWith(SpringRunner.class) @SpringBootTest public class ConfigPropertiesTest { @Autowired private ConfigProperties configProperties; @Test public void testValueProperties () { assertEquals("johuer" , configProperties.getName()); } }
使用方式2 组件(去除@Component
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package com.johuer.like.demo;import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties(prefix = "my") public class ConfigProperties { private String name; public String getName () { return name; } public void setName (String name) { this .name = name; } }
测试(增加@EnableConfigurationProperties(ConfigProperties.class)
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 package com.johuer.like.demo;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import static org.junit.Assert.assertEquals;@RunWith(SpringRunner.class) @SpringBootTest @EnableConfigurationProperties(ConfigProperties.class) public class ConfigPropertiesTest { @Autowired private ConfigProperties configProperties; @Test public void testValueProperties () { assertEquals("johuer" , configProperties.getName()); } }