Spring Boot 中获取配置的一些方法
发布于: 2020 年 08 月 31 日
通过 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;
@Configurationpublic class MyConfig {
@Bean public Student getStudent() { Student stu=new Student(1,"李四",false); return stu; }}
@RestControllerpublic class UserController { @Autowired private Student student; @RequestMapping("/") public String index() { return student.toString(); }}
//输出的页面内容为:Student{id=1, name='李四', sex=false}复制代码
通过 @Value 获取配置文件中的配置项
全局配置文件 application.properties定义如下:id=1name=李飞sex=true import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;
@RestControllerpublic 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 即可了。
还有一个原因,有可能缓存。等一会访问就不乱码了。
通过 @ConfigurationProperties 注入属性
people.id=2people.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;
@RestControllerpublic class UserController {
@Autowired private People people;
@RequestMapping("/") public People index() {
return people; }}复制代码
获取自定义配置文件中的内容
# 自定义的配置文件 db.propertiesdb.username=rootdb.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; }}
@RestControllerpublic 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; }}
复制代码
小结:
根据具体的需要,使用相应的获取配置的方式。
划线
评论
复制
发布于: 2020 年 08 月 31 日阅读数: 52
Geek_416be1
关注
还未添加个人签名 2020.08.27 加入
还未添加个人简介











评论