获取 properties 文件属性
1、在哪里使用哪里获取
@RestController
@RequestMapping("/message")
@PropertySource("classpath:commonConfig.properties")
public class MessageController {
@Autowired
public AsyncTask asyncTask;
@Value("${com.name}")
public String name;
@Value("${com.password}")
public String password;
@GetMapping(value = "/msg")
public String receiveMessage(){
return name + ":" + password;
}
}
2、定义公共类
- 定义
@Component
@Data
@PropertySource("classpath:commonConfig.properties") //默认的配置文件加载可以省略
//@ConfigurationProperties(prefix = "com") //添加可以省去属性@Value注解
public class CommonConfig{
@Value("${com.name}")
public String name;
@Value("${com.password}")
public String password;
- 调用
@RestController
@RequestMapping("/message")
public class MessageController {
@Autowired
public CommonConfig commonConfig;
@GetMapping(value = "/msg")
public String receiveMessage(){
System.out.println(msg.toString());
return commonConfig.getName() + ":" + commonConfig.getPassword();
}
}