How to customize @ value function in spring 4
preface
This article mainly introduces the related content of spring 4 custom @ value function, using spring version 4.3 10. Release, I won't say much below. Let's take a look at the detailed introduction.
@In spring, value is very powerful. You can inject a configuration item, reference the bean in the container (call its method), or do some simple operations
The following is a simple demo that demonstrates the usage of @ value
app. properties
Run, output results
port : 9090
userCount : 10
max : 10
min : 3
This is the general usage for injecting a value.
So, can I give an expression or a specific value, which can help calculate the value of the expression? In other words, how to implement a @ value function?
The method is as follows:
The specific use is as follows:
The operation output is as follows:
one thousand one hundred and twenty-one
nine thousand and ninety
ten
ten
three
It is found that @ value has been implemented
Finally, some people may have questions. What's the use of this? Isn't it good for me to use @ value directly?
For most scenarios, you can use @ value directly. However, there are some special scenarios that @ value can't do
For example, we define an annotation
This annotation requires a cron expression. Our requirement is that the user can directly use a cron expression or reference a configuration item (configure the value into the configuration file)
for instance
In this case, @ value can't do it, but you can use my above solution.
summary
The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.