Spring Boot 中获取配置的一些方法

用户头像
Geek_416be1
关注
发布于: 2020 年 08 月 31 日
  1. 通过Spring框架的@Configuration和@Bean来定义配置类对象,然后在需要的时候通过@Autowired注入配置类对象,进行操作。操作示例如下:

public class Student {
private int id;
private String name;
private boolean sex;
public Student()
{
}
public Student(int id, String name, boolean sex) {
this.id = id;
this.name = name;
this.sex = sex;
}
@Override
public String toString() {
final StringBuffer sb = new StringBuffer("Student{");
sb.append("id=").append(id);
sb.append(", name='").append(name).append('\'');
sb.append(", sex=").append(sex);
sb.append('}');
return sb.toString();
}
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyConfig {
@Bean
public Student getStudent()
{
Student stu=new Student(1,"李四",false);
return stu;
}
}
@RestController
public class UserController {
@Autowired
private Student student;
@RequestMapping("/")
public String index()
{
return student.toString();
}
}
//输出的页面内容为:Student{id=1, name='李四', sex=false}



  1. 通过@Value获取配置文件中的配置项

全局配置文件 application.properties定义如下:
id=1
name=李飞
sex=true
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Value("${id}")
private int id;
@Value("${name}")
private String name;
@Value("${sex}")
private boolean sex;
@RequestMapping("/")
public String index()
{
Student student=new Student(id,name,sex);
return student.toString();
}
}
//此时输出
// Student{id=1, name='���', sex=true}
// 问题中文乱码



修改编码,如下:



然后,再修改application.properties,确保正确的中文输入,接着

再运行项目,输出结果如下:

Student{id=1, name='å¼ ä¸‰', sex=true}

还是乱码,这是

注意:



如果刚修改完,重启项目还是乱码。





在idea右下角,CRLF修改为CR然后在访问,就不乱码了。再次切换为CRLF即可了。



还有一个原因,有可能缓存。等一会访问就不乱码了。



  1. 通过@ConfigurationProperties注入属性

people.id=2
people.name=李四
people.sex=true
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix ="people")
public class People {
private int id;
private String name;
private boolean sex;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private People people;
@RequestMapping("/")
public People index()
{
return people;
}
}



  1. 获取自定义配置文件中的内容

# 自定义的配置文件 db.properties
db.username=root
db.password=123456




import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "db")
@PropertySource("database.properties")
public class DBConfig {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
@RestController
public class UserController {
@Autowired
private People people;
@Autowired
private DBConfig dbConfig;
@RequestMapping("/")
public People index()
{
System.out.println(dbConfig.getUsername());
System.out.println(dbConfig.getPassword());
return people;
}
}



小结:

根据具体的需要,使用相应的获取配置的方式。



用户头像

Geek_416be1

关注

还未添加个人签名 2020.08.27 加入

还未添加个人简介

评论

发布
暂无评论
Spring Boot中获取配置的一些方法