前言
本文主要給大家介紹了關(guān)于Spring4自定義@Value功能的相關(guān)內(nèi)容,使用的Spring版本4.3.10.RELEASE,下面話不多說了,來一起看看詳細(xì)的介紹吧。
@Value在Spring中,功能非常強(qiáng)大,可以注入一個(gè)配置項(xiàng),可以引用容器中的Bean(調(diào)用其方法),也可以做一些簡(jiǎn)單的運(yùn)算
如下的一個(gè)簡(jiǎn)單demo,演示@Value的用法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
import org.springframework.stereotype.Service; /** * 測(cè)試Bean */ @Service ( "userService" ) public class UserService { public int count() { return 10 ; } public int max( int size) { int count = count(); return count > size ? count : size; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class AppRunner implements InitializingBean { /** * 引用一個(gè)配置項(xiàng) */ @Value ( "${app.port}" ) private int port; /** * 調(diào)用容器的一個(gè)bean的方法獲取值 */ @Value ( "#{userService.count()}" ) private int userCount; /** * 調(diào)用容器的一個(gè)bean的方法,且傳入一個(gè)配置項(xiàng)的值作為參數(shù) */ @Value ( "#{userService.max(${app.size})}" ) private int max; /** * 簡(jiǎn)單的運(yùn)算 */ @Value ( "#{${app.size} <= '12345'.length() ? ${app.size} : '12345'.length()}" ) private int min; //測(cè)試 public void afterPropertiesSet() throws Exception { System.out.println( "port : " + port); System.out.println( "userCount : " + userCount); System.out.println( "max : " + max); System.out.println( "min : " + min); } } |
app.properties
1
2
3
|
app.port= 9090 app.size= 3 |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.PropertySource; @ComponentScan @PropertySource ( "classpath:app.properties" ) public class App { public static void main( String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(App. class ); context.close(); } } |
運(yùn)行,輸出結(jié)果
port : 9090
userCount : 10
max : 10
min : 3
一般的用法就是這樣,用于注入一個(gè)值。
那么,能否做到,我給定一個(gè)表達(dá)式或者具體的值,它能幫忙計(jì)算出表達(dá)式的值呢? 也就是說,實(shí)現(xiàn)一個(gè)@Value的功能呢?
方法如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
import org.springframework.beans.factory.config.BeanExpressionContext; import org.springframework.beans.factory.config.BeanExpressionResolver; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.context.expression.StandardBeanExpressionResolver; public class ValueUtil { private static final BeanExpressionResolver resolver = new StandardBeanExpressionResolver(); /** * 解析一個(gè)表達(dá)式,獲取一個(gè)值 * @param beanFactory * @param value 一個(gè)固定值或一個(gè)表達(dá)式。如果是一個(gè)固定值,則直接返回固定值,否則解析一個(gè)表達(dá)式,返回解析后的值 * @return */ public static Object resolveExpression(ConfigurableBeanFactory beanFactory, String value) { String resolvedValue = beanFactory.resolveEmbeddedValue(value); if (!(resolvedValue.startsWith( "#{" ) && value.endsWith( "}" ))) { return resolvedValue; } return resolver.evaluate(resolvedValue, new BeanExpressionContext(beanFactory, null )); } } |
具體使用如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.PropertySource; @ComponentScan @PropertySource ( "classpath:app.properties" ) public class App { public static void main( String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(App. class ); //計(jì)算一個(gè)具體的值(非表達(dá)式) System.out.println(ValueUtil.resolveExpression(context.getBeanFactory(), "1121" )); //實(shí)現(xiàn)@Value的功能 System.out.println(ValueUtil.resolveExpression(context.getBeanFactory(), "${app.port}" )); System.out.println(ValueUtil.resolveExpression(context.getBeanFactory(), "#{userService.count()}" )); System.out.println(ValueUtil.resolveExpression(context.getBeanFactory(), "#{userService.max(${app.size})}" )); System.out.println(ValueUtil.resolveExpression(context.getBeanFactory(), "#{${app.size} <= '12345'.length() ? ${app.size} : '12345'.length()}" )); context.close(); } } |
運(yùn)行輸出如下:
1121
9090
10
10
3
發(fā)現(xiàn)已經(jīng)實(shí)現(xiàn)了@Value的功能
最后,可能有人就有疑問了,這有什么用呢?我直接用@Value難道不好嗎?
對(duì)于大部分場(chǎng)景下,的確直接用@Value就可以了。但是,有些特殊的場(chǎng)景,@Value做不了
比如說,我們定義一個(gè)注解
1
2
3
4
5
|
@Retention (RUNTIME) @Target (TYPE) public @interface Job { String cron(); } |
這個(gè)注解需要一個(gè)cron的表達(dá)式,我們的需求是,使用方可以直接用一個(gè)cron表達(dá)式,也可以支持引用一個(gè)配置項(xiàng)(把值配置到配置文件中)
比如說
1
2
|
@Job (cron = "0 0 12 * * ?" ) @Job (cron = "${app.job.cron}" ) |
這種情況@Value就做不到,但是,可以用我上面的解決方案。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)服務(wù)器之家的支持。
原文鏈接:http://blog.csdn.net/mn960mn/article/details/77430685