二、Spring配置文件读取

 

  在开发是时候我们使用Spring,主要是他为我们的开发提供很多便捷的方式,控制反转,依赖注入和面向切面编程是Spring的三大核心思想,配置文件读取也可以说是一种依赖注入,只不过这个是从配置文件中依赖注入进来的,那如何见配置文件中数据注入到bean 中呢?
(java学习交流③群:256909960,欢迎加入)
一、通过@Vaule来配置
  这种配置的是通过Spring将配置文件加载后,在属性上使用@Value注解将对应的值注入进来
 
1、pom文件
  需要的依赖
<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>

 

(java学习交流③群:256909960,欢迎加入)
2、配置类
  在这里就直接用Spring注解进行配置了,xml配置方式后续在写
@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}

(java学习交流③群:256909960,欢迎加入)
3、调用测试
  
@ComponentScan//1
public class AppConfiguration {
    
    public static void main(String[] args) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfiguration.class);
        applicationContext.getBean(ValueProperties.class).hello();
    }

1、扫描包,这个类要在上面ValueProperties类的同级或更上层的包中

(java学习交流③群:256909960,欢迎加入)
二、通过@ConfigurationProperties来配置
  这个是Springboot中的注解
 
1、pom文件
  
<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>

 

(java学习交流③群:256909960,欢迎加入)
2、配置类
  
@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; }

配置文件

server.irich.name=张国荣
server.irich.name=45
配置文件

 

1、向容器中加入这个bean

2、加载配置文件中,prefix是只加载文件中指定前缀的数据

一定要有setter方法

注:1.5以后@ConfigurationProperties没有了locations这个功能,但他可以和@PropertySource、@Import组合使用

(java学习交流③群:256909960,欢迎加入)
3、调用测试
  
@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放在一起使用

(java学习交流③群:256909960,欢迎加入)
三、读取外部文件(非根目录)
  
  其实就是把@PropertyResource("classpath:irich.properties")中的classpath该成file
读取项目根目录下的配置@PropertyResource("classpath:irich.properties")
读取外部文件@PropertyResource("file:/temp/data/config/irich.properties")
(java学习交流③群:256909960,欢迎加入)
四、读取yml中的其他类型数据
  yml和properties是有一定的区别的,在yml中我们可以定义其他数据类型list、map、等
 
1、配置文件application.yml
  
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

 

(java学习交流③群:256909960,欢迎加入)
2、读取yml配置文件的类
  在项目中配置文件数据注入的类
@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;  
    } 
}

 

(java学习交流③群:256909960,欢迎加入)
3、单元测试类
  单元测试
@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()));
    }
}

 

(java学习交流③群:256909960,欢迎加入)
4、结果
  
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"}

 

(java学习交流③群:256909960,欢迎加入)
posted @ 2017-09-07 11:56  irich  阅读(550)  评论(0编辑  收藏  举报
(全栈工程师③群:256909960,欢迎加入)全栈工程师③群