1.propertie 配置读取数据
- /**
- * 通过 value 取配置文件中的数据
- */
- @Component
- @PropertySource(value = {"config/db-config.properties"})
- public class InfoConfig1 {
- @Value("${db.username}")
- private String useranme;
- @Value("${db.password}")
- private String password;
- public String getUseranme() {
- return useranme;
- }
- public void setUseranme(String useranme) {
- this.useranme = useranme;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- @Override
- public String toString() {
- return "";
- }
- }
2. 取值
- @RestController
- public class InConfigController {
- @Autowired
- private Environment env;
- @Value("${db.username}")
- private String username;
- @RequestMapping(value = "/info")
- public String getValue() {
- String username = env.getProperty("db.username");
- String password = env.getProperty("db.password");
- return username;
- }
- @RequestMapping(value = "/info2")
- public void getValue1() {
- System.out.println(username);
- }
- }
来源: http://www.bubuko.com/infodetail-3074781.html