spring 自定義讓@Value被解析到
spring 自定義讓@Value解析到
@Value 可以給字段賦值
背景
@Value通常與@PropertySource(value = “db.properties”) 組合使用讀取配置注入參數,那如果我們的值是其它存儲,如何才能自動賦值
實現原理
實現很簡單
//自動注入此對象
@Autowired
private Environment environment;
@PostConstruct
public void init() {
//拿到些對象
MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment).getPropertySources();
PropertySourceFactory factory = BeanUtils.instantiateClass(DefaultPropertySourceFactory.class);
//構造pathResource
PathResource pathResource = new PathResource("/Users/xx/soft/sp.properties");
try {
org.springframework.core.env.PropertySource<?> sd = factory.createPropertySource("sd", new EncodedResource(pathResource));
//設置值
propertySources.addFirst(sd);
} catch (IOException e) {
e.printStackTrace();
}
}
主要是通過代碼得到PropertySource 這個對象,然后得到environment這個對象,設置值就可以了
Spring4自定義@Value功能
本文章使用的Spring版本4.3.10.RELEASE
@Value在Spring中,功能非常強大,可以注入一個配置項,可以引用容器中的Bean(調用其方法),也可以做一些簡單的運算
如下的一個簡單demo,
演示@Value的用法
import org.springframework.stereotype.Service;
/**
* 測試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 {
/**
* 引用一個配置項
*/
@Value("${app.port}")
private int port;
/**
* 調用容器的一個bean的方法獲取值
*/
@Value("#{userService.count()}")
private int userCount;
/**
* 調用容器的一個bean的方法,且傳入一個配置項的值作為參數
*/
@Value("#{userService.max(${app.size})}")
private int max;
/**
* 簡單的運算
*/
@Value("#{${app.size} <= '12345'.length() ? ${app.size} : '12345'.length()}")
private int min;
//測試
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();
}
}
運行,輸出結果
port : 9090
userCount : 10
max : 10
min : 3
一般的用法就是這樣,用于注入一個值。
那么,能否做到,我給定一個表達式或者具體的值,它能幫忙計算出表達式的值呢? 也就是說,實現一個@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();
/**
* 解析一個表達式,獲取一個值
* @param beanFactory
* @param value 一個固定值或一個表達式。如果是一個固定值,則直接返回固定值,否則解析一個表達式,返回解析后的值
* @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);
//計算一個具體的值(非表達式)
System.out.println(ValueUtil.resolveExpression(context.getBeanFactory(), "1121"));
//實現@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();
}
}
運行輸出如下:
1121
9090
10
10
3
發(fā)現已經實現了@Value的功能
最后,可能有人就有疑問了,這有什么用呢?我直接用@Value難道不好嗎?
對于大部分場景下,的確直接用@Value就可以了。但是,有些特殊的場景,@Value做不了
比如說
我們定義一個注解
@Retention(RUNTIME)
@Target(TYPE)
public @interface Job {
String cron();
}
這個注解需要一個cron的表達式,我們的需求是,使用方可以直接用一個cron表達式,也可以支持引用一個配置項(把值配置到配置文件中)
比如說
@Job(cron = "0 0 12 * * ?")
@Job(cron = "${app.job.cron}")
這種情況@Value就做不到,但是,可以用我上面的解決方案。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
mybatis-generator生成多次重復代碼問題以及解決
在使用MySQL數據庫時,如果多個數據庫中存在相同表名,即使在URL中配置了數據庫名,也可能導致數據互相影響,解決這一問題的方法是在mapper-generator-config.xml文件中添加catalog屬性,明確指定逆向工程代碼所涉及表的數據庫名2024-10-10
SpringBoot創(chuàng)建動態(tài)定時任務的幾種方式小結
SpringBoot提供了多種實現定時任務的方式,包括使用@Scheduled注解、SchedulingConfigurer接口、TaskScheduler接口和Quartz框架,@Scheduled適合簡單的定時任務,文中通過代碼示例介紹的非常詳細,需要的朋友可以參考下2024-10-10

