二、Spring配置文件读取
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.7.RELEASE</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging-api</artifactId> <version>1.1</version> </dependency>
@Component //1 @PropertySource("classpath:aaa.properties")//2 public class ValueProperties { @Value("${name}")//3 private String name; @Value("${age}")//4 private Integer age; public void hello(){ System.out.println("my name : "+name+"| age: "+age); } }
1、@component 让Spring能够扫描到这个包
2、@PropertiesSource 读取加载classpath下的配置文件(只能是properties)
3、注入配置文件中的数据name 和age,
例如配置文件中的数据是这样的host.ip,那么注入时应该这样写${host.ip}
@ComponentScan//1 public class AppConfiguration { public static void main(String[] args) { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfiguration.class); applicationContext.getBean(ValueProperties.class).hello(); }
1、扫描包,这个类要在上面ValueProperties类的同级或更上层的包中
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> </dependency>
@Component//1 @ConfigurationProperties(prefix = "server.irich")//2
//这里不可以用@PropertySource加载配置文件
public class BootConfiguration { private String name; private Integer age; public void hello(){ System.out.println("my name : "+name+"| age: "+age); } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; }
配置文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
server.irich.name=张国荣
server.irich.name=45
1、向容器中加入这个bean
2、加载配置文件中,prefix是只加载文件中指定前缀的数据
一定要有setter方法
注:1.5以后@ConfigurationProperties没有了locations这个功能,但他可以和@PropertySource、@Import组合使用
@ComponentScan //1 @EnableConfigurationProperties //2 @PropertySource("classpath:aaa.properties") //3 //@SpringBootApplication public class BootConfigurationApp { public static void main(String[] args) { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(BootConfigurationApp.class); applicationContext.getBean(BootConfiguration.class).hello(); // SpringApplication.run(BootConfigurationApp.class,args); } }
1、配置扫描包
2、当@EnableConfigurationProperties注解应用到你的@Configuration时,任何被@ConfigurationProperties注解的beans将自动被Environment属性配置
3、在@ConfigurationProperties为属性赋值前加载配置文件,不可以将需要加在的配置文件写在和@ConfigurationProperties相同的位置
注:1)、@ConfigurationProperties一般都是和@EnableConfigurationProperties一起使用,这样就可以实现自动配置了,
2)、现在1.5版本@ConfigurationProperties不支持location方法了,可以使用@PropertyResource来代替,
3)、不过要注意这个加载配置文件一定要在@ConfigurationProperties他修饰的类加载之前;简单点说就是@PropertyResource和@Configuration放在一起使用
myProps: #自定义的属性和值
simpleProp: simplePropValue
arrayProps: 1,2,3,4,5
listProp1:
- name: abc
value: abcValue
- name: efg
value: efgValue
listProp2:
- config2Value1
- config2Vavlue2
mapProps:
key1: value1
key2: value2
@ConfigurationProperties(prefix="myProps") //application.yml中的myProps下的属性 public class YmlConfig { private String simpleProp; private String[] arrayProps; private List<Map<String, String>> listProp1 = new ArrayList<>(); //接收prop1里面的属性值 private List<String> listProp2 = new ArrayList<>(); //接收prop2里面的属性值 private Map<String, String> mapProps = new HashMap<>(); //接收prop1里面的属性值 public String getSimpleProp() { return simpleProp; } public void setSimpleProp(String simpleProp) { this.simpleProp = simpleProp; } public List<Map<String, String>> getListProp1() { return listProp1; } public List<String> getListProp2() { return listProp2; } public String[] getArrayProps() { return arrayProps; } public void setArrayProps(String[] arrayProps) { this.arrayProps = arrayProps; } public Map<String, String> getMapProps() { return mapProps; } public void setMapProps(Map<String, String> mapProps) { this.mapProps = mapProps; } }
@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = ReadApplication.class) public class ReadApplicationYmlTests { @Autowired private YmlConfig ymlConfig; @Test public void testDisplayYmlValue() throws JsonProcessingException { System.out.println("simpleProp: " + ymlConfig.getSimpleProp()); ObjectMapper objectMapper = new ObjectMapper(); System.out.println("arrayProps: " + objectMapper.writeValueAsString(ymlConfig.getArrayProps())); System.out.println("listProp1: " + objectMapper.writeValueAsString(ymlConfig.getListProp1())); System.out.println("listProp2: " + objectMapper.writeValueAsString(ymlConfig.getListProp2())); System.out.println("mapProps: " + objectMapper.writeValueAsString(ymlConfig.getMapProps())); } }
simpleProp: simplePropValue
arrayProps: ["1","2","3","4","5"]
listProp1: [{"name":"abc","value":"abcValue"},{"name":"efg","value":"efgValue"}]
listProp2: ["config2Value1","config2Vavlue2"]
mapProps: {"key1":"value1","key2":"value2"}