In our work, we usually use @Value for property injection for configuration information or property that is easily changed. This way, when properties change, we can modify the configuration file directly, without having to make changes in the code.
background
A recent development task used the @Value annotation to inject properties.
The development content of this time is mainly to expose a picture path, the database is stored in the relative path of the picture, so every time the query is to call the encapsulated tool class for domain name Mosaic, and the domain name is injected through @value in the tool class
The configuration file
image-domain-name.prefix=http:xxxx.com
Copy the code
Utility class
@Getter @Component @Slf4j public class ImgUtil { @Value("image-domain-name.prefix:") private static String PREFIX; Private ImgUtil() {} public static String addImgPre(String imgUrl) {Copy the code
Originally a very simple development task, the results of a test found that the picture is not displayed, a look at the path display is
null/xxxx/b9a1aa85-9b40-4e18-8851-8965df2a88f8
Copy the code
When you see NULL, your first thought is that the property did not inject successfully.
After debugging, it is found that @value does not support assigning values to static variables directly.
The reason for not injecting
In fact, the reason is very simple, serious thinking will know the reason.
The @value annotation relies on the property’s set method for injection, whereas static modified properties are class properties with no set method
The solution
1. Manually assign values to the set method
Injecting static variables using non-static setter methods will do property injection at Spring load time
@Getter @Component @Slf4j public class ImgUtil { private static String PREFIX; private ImgUtil() {} @Value("image-domain-name.prefix:") public void setPreFix(String preFix) { ImgUtil.PREFIX = preFix; } public static String addImgPre(String imgUrl) {//Copy the code
2. Constructor assignment
It’s almost the same as set
@Getter @Component @Slf4j public class ImgUtil { private static String PREFIX; private ImgUtil() {} @Value("image-domain-name.prefix:") public ImgUtil(String preFix) { ImgUtil.PREFIX = preFix; } public static String addImgPre(String imgUrl) {//Copy the code
3, @ PostConstruct
This annotation is part of the Common Annotations API, which is also part of the JDK module javax.annotation-API. Spring calls the @postconstruct annotated method only once, right after the bean property is initialized. Remember that these methods will run even if there is nothing to initialize.
Methods annotated @postconstruct can have any level of access, but cannot be static
@Getter @Component @Slf4j public class ImgUtil { private static String PREFIX; private ImgUtil() {} @Value("image-domain-name.prefix:") private String PREFIX_TEMP; @PostConstruct public void init(){ PREFIX = PREFIX_TEMP; } public static String addImgPre(String imgUrl) {//Copy the code
There are other ways to inject static properties, such as using @ConfigurationProperties
conclusion
The @Value annotation can be used to inject properties of beans in the Spring container. It can be used on properties, constructors, and general methods.