作者:欢乐天天快乐一生 | 来源:互联网 | 2023-08-26 08:22
Valuemy.testdemoprivatestaticStringtest;Value(${my.test})publicvoidsetTest(Stringtest)
@Value
my.test=demo
private static String test;@Value("${my.test}")public void setTest(String test) {TestController.test = test;}//类上需要打上@Component注解,把类交给Spring来管理
//把setter方法 修饰的static去掉
//如果在别的类中注入这个类,这个类不能是new出来的,要用@Autowired
静态代码块读取
clientid=client124
private static String clientid;static {Properties prop = new Properties();InputStream in = Object.class.getResourceAsStream("/application.properties");try {prop.load(in);clientid = prop.getProperty("clientid").trim();} catch (IOException e) {e.printStackTrace();}logger.info("clientid:{}",clientid);init(clientid);start();}2020-03-27 11:56:21.979 INFO 103164 --- [ main] c.x.elevator.platform.mqtt.PushCallback : clientid:client124