服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - 聊聊@value注解和@ConfigurationProperties注解的使用

聊聊@value注解和@ConfigurationProperties注解的使用

2022-01-06 11:04qq_29914229 Java教程

这篇文章主要介绍了@value注解和@ConfigurationProperties注解的使用,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

@value注解和@ConfigurationProperties注解

@value读取默认配置

yml文件内容如下(装了STS插件以后即可直接使用,改后缀就行了)

?
1
2
3
4
5
6
7
user:
  username: xiongda
  sex: man
  age: 20
school:
  name: xtu
  location: hunan

备注:一定要注意之间要留空格,发现颜色变绿色了才是正确的格式,这个坑我刚踩

?
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
26
27
28
29
30
31
package com.example.demo.service.impl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.example.demo.service.ReadConfigService;
@Service
public class ReadConfigServiceImpl implements ReadConfigService {   
    @Value(value="${user.username}")
    private String username;
    
    @Value(value="${user.sex}")
    private String sex;
    
    @Value(value="${user.age}")
    private String age;
    
    @Value(value="${school.name}")
    private String name;
    
    @Value(value="${school.location}")
    private String location;
    
    @Override
    public String getUserMessage() {
        return "user ="+username+" sex ="+sex+" age="+age;
    }
 
    @Override
    public String getAuthorMessage() {
        return "schoolname="+name+"location="+location;
    }   
}

@ConfigurationProperties读取默认配置

?
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
26
27
28
29
30
31
32
33
package com.example.demo.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix="user")
public class HelloConfig {
    private String username;   
    private String sex;   
    private String age;
    public String getUsername() {
        return username;
    }
 
    public void setUsername(String username) {
        this.username = username;
    }
 
    public String getSex() {
        return sex;
    }
 
    public void setSex(String sex) {
        this.sex = sex;
    }
 
    public String getAge() {
        return age;
    }
 
    public void setAge(String age) {
        this.age = age;
    }   
}

调用的controller层

?
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
26
27
package com.example.demo.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.config.HelloConfig;
import com.example.demo.service.ReadConfigService;
 
@RestController
@RequestMapping("hello")
public class HelloController {
    
    @Autowired
    ReadConfigService readConfigService;
    
    @Autowired
    HelloConfig helloConfig;
    
    @RequestMapping("/user")
    public String say() {
       return "username "+helloConfig.getUsername()+" sex "+helloConfig.getSex()+" age "+helloConfig.getAge();
    }
    
    @RequestMapping("/school")
    public String school() {
       return readConfigService.getAuthorMessage();
    }
}

@ConfigurationProperties和@Value使用上的一点区别

@ConfigurationProperties和@Value的一个共同点就是从配置文件中读取配置项。

发现有一点区别,我项目配置中并没有配置hello.msg ,当使用第一段代码时,启动后读取到msg为null,而第二段代码则会抛出异常。

第二段代码有个好处,就是防止我们配置项遗漏,当遗漏时,启动程序肯定出错,这样避免了一些因为遗漏配置项导致的BUG.

第一段代码

?
1
2
3
4
5
6
7
8
9
10
11
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties("hello")
public class HelloProperties {
    private String msg;
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
}

第二段代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Hello2Properties {
    @Value("${hello.msg}")
    private String msg;
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_29914229/article/details/81906406

延伸 · 阅读

精彩推荐