ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

springboot+value静态属性获取配置文件中的值的操作方法

springboot+value静态属性获取配置文件中的值的操作方法

1.配置类需要让spring管理
2.set方法不要加static
3.如果静态属性是private修饰,则在使用的时候,需要 类名.getXXX方法
如果静态属性是public修饰,则在使用的时候,需要 类名.属性名

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class TestConfig {private static String url ;public static String getUrl() {return url;}@Value("${sendMessUrl}")public  void setUrl(String url) {TestConfig.url = url;}
}
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class TestConfigController {@GetMapping("/testConfig")public String testConfig() {return TestConfig.getUrl();}
}
sendMessUrl: XXX
返回列表