SpringBoot reads the yml file configuration and fails to obtain the value

SpringBoot reads the yml file configuration and fails to obtain the value

springboot reads the value in yml:

yml file:

system:
    url:127.0.0.1

kind:

 //类中声明为成员变量,之后就可以在方法中使用了
    @Value("${system.url}")
    private String url;

If the value cannot be obtained, please check whether the pom project has incorrect dependencies.

**

1. The spring component rewrites the construction method and references @value in the construction method as null.

Since the spring instantiation sequence is to execute the constructor first, and then inject the member variables, the order is to execute the constructor first, and then inject the member variables, so the value of ing instantiation is null. The solution is: write another constant class, in the
constant Just reference @value in the class, and then reference the variables of the constant class in the constructor.

Second, use new objects instead of @Autowired when calling spring components.

3. Use final or static to modify member variables

Fourth, the @value referenced in spring mvc is null

Spring mvc is a sub-container of spring and needs to import configuration files in both configuration files
<context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true"/>**

Original blog link: https://blog.csdn.net/qq_31183727/article/details/82493575?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522159281103419726869019052%2522%252C%25 22scm%2522%253A%252220140713.130102334…% 2522%257D&request_id=159281103419726869019052&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2 all sobaiduend~default-2-82493575.first_rank_ecpm_v1_pc_rank_v3&utm_term=springboot% E9%A1%B9%E7%9B%AE%40Value%E6%B3% A8%E8%A7%A3%E8%AF%BB%E5%8F%96%E4%B8%8D%E5%88%B0yml%E4%B8%AD%E9%85%8D%E7%BD%AE

Guess you like

Origin blog.csdn.net/weixin_44021888/article/details/106903259#comments_28177422