SpringBoot中@PropertySource和@ImportResource以及@Bean


@PropertySource

  • 加载指定的配置文件
  • 只能加载*.properties文件,不能加载yaml文件

新建一个user.properties

  1. user.nickname=张三
  2. user.age=19
  3. user.sex=男
  4. user.maps.weight=70
  5. user.maps.height=170
  6. user.address.addr=重庆市渝中区

UserBean

  1. @Component
  2. @PropertySource(value = {"classpath:user.properties"})
  3. @ConfigurationProperties(prefix = "user")
  4. public class User {
  5. private String nickname;
  6. private Integer age;
  7. private char sex;
  8. private Map<String,Integer> maps;
  9. private Address address;
  10. ...
  11. }

@ImportResource

  • 导入Spring的配置文件,让配置文件里面的内容生效

SpringBoot中编写的Spring配置文件是不能自动识别的

在主配置类上加入@ImportResource

  1. @ImportResource(locations = {"classpath:beans.xml"})

SpringBoot给容器添加组件的方式

1、配置类 == Spring配置文件 通过@Configuration声明
2、使用@Bean给容器添加组件,组件id默认为方法名

例子

  1. package com.atgenee.demo.service;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. @Configuration
  5. public class Hello {
  6. @Bean
  7. public Hello helloService() {
  8. System.out.println("添加组件");
  9. return new Hello();
  10. }
  11. }

测试

  1. package com.atgenee.demo;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.boot.test.context.SpringBootTest;
  6. import org.springframework.context.ApplicationContext;
  7. import org.springframework.test.context.junit4.SpringRunner;
  8. @RunWith(SpringRunner.class)
  9. @SpringBootTest
  10. public class HelloServiceApplicationTests {
  11. // @Autowired
  12. // private Hello hello;
  13. //
  14. // @Test
  15. // public void hello() {
  16. // hello.helloService();
  17. // }
  18. @Autowired
  19. private ApplicationContext ioc;
  20. @Test
  21. public void hello() {
  22. ioc.getBean("helloService");
  23. }
  24. }

请注意

若配置类已经加了@Bean注解,此时配置类中的方法名不能跟类名一样,也就是上面的Hello类中不能定义hello()的方法,否则报错


通过自定义工厂来实现自定义yaml文件加载

新建一个cat.yml文件

  1. cat:
  2. age: 3
  3. height: 20
  4. weight: 5

工厂类

  1. package com.atgenee.demo.factory;
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.util.Properties;
  5. import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
  6. import org.springframework.core.env.PropertiesPropertySource;
  7. import org.springframework.core.env.PropertySource;
  8. import org.springframework.core.io.support.EncodedResource;
  9. import org.springframework.core.io.support.PropertySourceFactory;
  10. import org.springframework.lang.Nullable;
  11. public class YamlPropertySourceFactory implements PropertySourceFactory {
  12. @Override
  13. public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException {
  14. Properties propertiesFromYaml = loadYamlIntoProperties(resource);
  15. String sourceName = name != null ? name : resource.getResource().getFilename();
  16. return new PropertiesPropertySource(sourceName, propertiesFromYaml);
  17. }
  18. private Properties loadYamlIntoProperties(EncodedResource resource) throws FileNotFoundException {
  19. try {
  20. YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
  21. factory.setResources(resource.getResource());
  22. factory.afterPropertiesSet();
  23. return factory.getObject();
  24. } catch (IllegalStateException e) {
  25. // for ignoreResourceNotFound
  26. Throwable cause = e.getCause();
  27. if (cause instanceof FileNotFoundException)
  28. throw (FileNotFoundException) e.getCause();
  29. throw e;
  30. }
  31. }
  32. }

新建配置类 Cat,配合@PropertySource 注解使用

  1. package com.atgenee.demo.config;
  2. import com.atgenee.demo.factory.YamlPropertySourceFactory;
  3. import org.springframework.boot.context.properties.ConfigurationProperties;
  4. import org.springframework.context.annotation.PropertySource;
  5. import org.springframework.stereotype.Component;
  6. @Component
  7. @PropertySource(factory = YamlPropertySourceFactory.class, value = "classpath:cat.yml")
  8. @ConfigurationProperties(prefix = "cat")
  9. public class Cat {
  10. private int age;
  11. private Double weight;
  12. private Double height;
  13. public int getAge() {
  14. return age;
  15. }
  16. public void setAge(int age) {
  17. this.age = age;
  18. }
  19. public Double getWeight() {
  20. return weight;
  21. }
  22. public void setWeight(Double weight) {
  23. this.weight = weight;
  24. }
  25. public Double getHeight() {
  26. return height;
  27. }
  28. public void setHeight(Double height) {
  29. this.height = height;
  30. }
  31. @Override
  32. public String toString() {
  33. return "Cat{" +
  34. "age=" + age +
  35. ", weight=" + weight +
  36. ", height=" + height +
  37. '}';
  38. }
  39. }

Cat测试类

  1. package com.atgenee.demo;
  2. import com.atgenee.demo.config.Cat;
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.test.context.SpringBootTest;
  7. import org.springframework.test.context.junit4.SpringRunner;
  8. @RunWith(SpringRunner.class)
  9. @SpringBootTest
  10. public class CatApplicationTests {
  11. @Autowired
  12. private Cat cat;
  13. @Test
  14. public void hei() {
  15. System.out.println(cat);
  16. }
  17. }

控制台输出

  1. Cat{age=3, weight=5.0, height=20.0}

0
1
0

添加评论

正在回复:
取消
0
0
1
0