Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the reasons for the failure of spring@value injection profile value

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to analyze the reasons for the failure of spring@value injection profile values, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

Reasons for failure of spring@value injection profile value

Today I wrote a system.propertities configuration file that defines a variable host=localhost.

Then add the load configuration to the spring configuration file

Write this in service

@ Value ("${host}") private static String host

But can not get, all kinds of information, and finally found that it is the reason for the static keyword

Spring@Value dependency injection is dependent on the set method

The set method is an ordinary object method, the static variable is a property of the class, and there is no set method.

Spring configuration file @ Value comment injection failed or null

When spring uses @ Value to inject values from application.properties into variables, it encounters

There are two problems: injection failure and injection value of null.

Solution

1. View maven dependencies

(if effective, do not take the next steps)

2. Add the comment @ PropertySource (value = "classpath:/application.properties") to the configuration file path.

(if effective, do not take the next steps)

3. Input the injected variable as the parameter of the construction method.

Code example

Maven dependence

Org.springframework spring-core 5.1.5.RELEASE org.springframework.boot spring-boot-starter 2.1.3.RELEASE

Config class

@ Configuration// declares the properties file location @ PropertySource (value = "classpath:/application.properties") public class DemoConfig {private String name; / / injects @ Value as a constructor parameter into public DemoConfig (@ Value ("${book.name}") String name) {this.name = name;} public void output () {System.out.println (name);}}

Main

@ SpringBootApplicationpublic class DemoApplication {public static void main (String [] args) {/ / SpringApplication.run (DemoApplication.class, args); AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (DemoConfig.class); DemoConfig service = context.getBean (DemoConfig.class); service.output ();}} problem resolution

1. Spring version problem. According to my experiment, there will be some injection problems in the version below 4.x.

2. The @ PropertySource (value = "classpath:/application.properties") annotation is not written, or the path is incorrect.

3. The .properties file is not placed in the resources folder.

Problem expansion

1. In addition to the application.properties file that comes with springboot, you can create your own test.properties, import other self-created attributes, and manage attributes.

After reading the above, do you have any further understanding of how to analyze the reasons for the failure of spring@value injection profile values? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report