Spring4如何自定義@Value功能詳解
前言
本文主要給大家介紹了關(guān)于Spring4自定義@Value功能的相關(guān)內(nèi)容,使用的Spring版本4.3.10.RELEASE,下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。
@Value在Spring中,功能非常強(qiáng)大,可以注入一個(gè)配置項(xiàng),可以引用容器中的Bean(調(diào)用其方法),也可以做一些簡(jiǎn)單的運(yùn)算
如下的一個(gè)簡(jiǎn)單demo,演示@Value的用法
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;
}
}
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
app.port=9090 app.size=3
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á)式的值呢? 也就是說(shuō),實(shí)現(xiàn)一個(gè)@Value的功能呢?
方法如下:
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));
}
}
具體使用如下:
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的功能
最后,可能有人就有疑問(wèn)了,這有什么用呢?我直接用@Value難道不好嗎?
對(duì)于大部分場(chǎng)景下,的確直接用@Value就可以了。但是,有些特殊的場(chǎng)景,@Value做不了
比如說(shuō),我們定義一個(gè)注解
@Retention(RUNTIME)
@Target(TYPE)
public @interface Job {
String cron();
}
這個(gè)注解需要一個(gè)cron的表達(dá)式,我們的需求是,使用方可以直接用一個(gè)cron表達(dá)式,也可以支持引用一個(gè)配置項(xiàng)(把值配置到配置文件中)
比如說(shuō)
@Job(cron = "0 0 12 * * ?")
@Job(cron = "${app.job.cron}")
這種情況@Value就做不到,但是,可以用我上面的解決方案。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Spring Boot2配置服務(wù)器訪問(wèn)日志過(guò)程解析
這篇文章主要介紹了Spring Boot2配置服務(wù)器訪問(wèn)日志過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
淺析Java.IO輸入輸出流 過(guò)濾流 buffer流和data流
這篇文章主要介紹了Java.IO輸入輸出流 過(guò)濾流 buffer流和data流的相關(guān)資料,本文給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10
IDEA 2021.2 激活教程及啟動(dòng)報(bào)錯(cuò)問(wèn)題解決方法
這篇文章主要介紹了IDEA 2021.2 啟動(dòng)報(bào)錯(cuò)及激活教程,文章開(kāi)頭給大家介紹了idea2021最新激活方法,關(guān)于idea2021啟動(dòng)報(bào)錯(cuò)的問(wèn)題小編也給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-10-10

