java 获取配置内容的3种方式

首先配置application.yaml

server:
port: 8082

address:
- beijing
- shanghai

name: lisi

person:
name: zhangsan
age: 18

msg1: 'hello \n world' #纯量
msg2: "hellow \n world"

1.@Value



controller测试文件
复制代码
 1 package com.itheima.springbootinit.controller;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.beans.factory.annotation.Value;
 5 import org.springframework.core.env.Environment;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.RequestMethod;
 8 import org.springframework.web.bind.annotation.RestController;
 9 
10 @RestController
11 public class HelloController {
12 
13     @Value("${name}")
14     private String name;
15 
16     @Value("${person.name}")
17     private String name2;
18 
19     @Value("${address[0]}")
20     private String address;
21 
22     @Value("${msg1}")
23     private String msg1;
24 
25     @Value("${msg2}")
26     private String msg2;
27 
28 
29     @RequestMapping("/hello")
30     public String hello(){
31         return "hello spring boot!";
32     }
33 
34     @RequestMapping(value = "/hello2",method = RequestMethod.GET)
35     public String hello2(){
36         System.out.println(name);
37         System.out.println(name2);
38         System.out.println(address);
39 
40         System.out.println(msg1);
41         System.out.println(msg2);
42 
43         return "hello2 spring boot!";
44     }
45 }
复制代码

结果:

 

 

 纯量的引号决定了是否转义字符串,单引号不转义,双引号转义

 

 

2.Environment

复制代码
package com.itheima.springbootinit.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Value("${name}")
    private String name;

    @Value("${person.name}")
    private String name2;

    @Value("${address[0]}")
    private String address;

    @Value("${msg1}")
    private String msg1;

    @Value("${msg2}")
    private String msg2;


    @Autowired
    private Environment env;

    @RequestMapping("/hello")
    public String hello(){
        return "hello spring boot!";
    }

    @RequestMapping(value = "/hello2",method = RequestMethod.GET)
    public String hello2(){
        System.out.println(env.getProperty("name"));
        System.out.println(env.getProperty("person.name"));
        System.out.println(env.getProperty("address[0]"));
        System.out.println(env.getProperty("msg1"));
        System.out.println(env.getProperty("msg2"));


        return "hello2 spring boot!";
    }
}
复制代码

结果:

 

 

 

 

3.@ConfigurationProperties

复制代码
package com.itheima.springbootinit.controller;

import com.itheima.springbootinit.pojo.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Value("${name}")
    private String name;

    @Value("${person.name}")
    private String name2;

    @Value("${address[0]}")
    private String address;

    @Value("${msg1}")
    private String msg1;

    @Value("${msg2}")
    private String msg2;


    @Autowired
    private Environment env;

    @Autowired  #自动注入获取配置文件中的person
    private Person person;

    @RequestMapping("/hello")
    public String hello(){
        return "hello spring boot!";
    }

    @RequestMapping(value = "/hello2",method = RequestMethod.GET)
    public String hello2(){
        System.out.println(person);


        return "hello2 spring boot!";
    }
}
复制代码

使用@ConfigurationProperties需要注意,当配置文件中存在同名属性时,需使用注解,否则会出现意料之外的结果,比如

创建Person.java

复制代码
package com.itheima.springbootinit.pojo;


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties
public class Person {

    private String name;
    private Integer age;

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public Person() {
    }
}
复制代码

结果

 

 

很明显,我们并没有得到我们想要的person的数据

在注解@ConfigurationProperties中加入prefix = "person"
复制代码
package com.itheima.springbootinit.pojo;


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "person")
public class Person {

    private String name;
    private Integer age;

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public Person() {
    }
}
复制代码

结果:

 

posted @   斯瓦勃洛  阅读(2120)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示