欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring 自定義讓@Value被解析到

 更新時間:2021年09月18日 12:26:35   作者:wending-Y  
這篇文章主要介紹了spring 自定義讓@Value被解析到,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

spring 自定義讓@Value解析到

@Value 可以給字段賦值

背景

@Value通常與@PropertySource(value = “db.properties”) 組合使用讀取配置注入?yún)?shù),那如果我們的值是其它存儲,如何才能自動賦值

實現(xiàn)原理

實現(xiàn)很簡單

//自動注入此對象 
 @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(調(diào)用其方法),也可以做一些簡單的運算

如下的一個簡單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;
 
 /**
  * 調(diào)用容器的一個bean的方法獲取值
  */
 @Value("#{userService.count()}")
 private int userCount;
 
 /**
  * 調(diào)用容器的一個bean的方法,且傳入一個配置項的值作為參數(shù)
  */
 @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

一般的用法就是這樣,用于注入一個值。

那么,能否做到,我給定一個表達式或者具體的值,它能幫忙計算出表達式的值呢? 也就是說,實現(xiàn)一個@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"));
     //實現(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();
    }
}

運行輸出如下:

1121
9090
10
10
3

發(fā)現(xiàn)已經(jīng)實現(xiàn)了@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就做不到,但是,可以用我上面的解決方案。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • 簡單了解Spring Cloud搭建Config過程實例

    簡單了解Spring Cloud搭建Config過程實例

    這篇文章主要介紹了簡單了解Spring Cloud搭建Config過程實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • Java線程啟動為什么要用start()而不是run()?

    Java線程啟動為什么要用start()而不是run()?

    這篇文章主要介紹了線程啟動為什么要用start()而不是run()?下面文章圍繞start()與run()的相關資料展開詳細內(nèi)容,具有一定的參考價值,西藥的小火熬版可以參考一下,希望對你有所幫助
    2021-12-12
  • IDEA設置JVM可分配內(nèi)存大小和其他參數(shù)的教程

    IDEA設置JVM可分配內(nèi)存大小和其他參數(shù)的教程

    這篇文章主要介紹了IDEA設置JVM可分配內(nèi)存大小和其他參數(shù)的教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • mybatis-generator生成多次重復代碼問題以及解決

    mybatis-generator生成多次重復代碼問題以及解決

    在使用MySQL數(shù)據(jù)庫時,如果多個數(shù)據(jù)庫中存在相同表名,即使在URL中配置了數(shù)據(jù)庫名,也可能導致數(shù)據(jù)互相影響,解決這一問題的方法是在mapper-generator-config.xml文件中添加catalog屬性,明確指定逆向工程代碼所涉及表的數(shù)據(jù)庫名
    2024-10-10
  • Java中常見的陷阱題及答案

    Java中常見的陷阱題及答案

    在電腦里找到一份當時學習JAVA時的筆記,看到一些現(xiàn)在已經(jīng)遺忘的細節(jié)。稍微整理了幾個,發(fā)出來與大家分享。這篇文章主要介紹了Java中常見的陷阱題及答案,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-01-01
  • HashMap原理及put方法與get方法的調(diào)用過程

    HashMap原理及put方法與get方法的調(diào)用過程

    這篇文章主要介紹了HashMap原理及put方法與get方法的調(diào)用過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java 實現(xiàn)攔截器Interceptor的攔截功能方式

    Java 實現(xiàn)攔截器Interceptor的攔截功能方式

    這篇文章主要介紹了Java 實現(xiàn)攔截器Interceptor的攔截功能方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java @Autowired注解底層原理詳細分析

    Java @Autowired注解底層原理詳細分析

    @Autowired注解可以用在類屬性,構造函數(shù),setter方法和函數(shù)參數(shù)上,該注解可以準確地控制bean在何處如何自動裝配的過程。在默認情況下,該注解是類型驅(qū)動的注入
    2022-11-11
  • SpringBoot創(chuàng)建動態(tài)定時任務的幾種方式小結

    SpringBoot創(chuàng)建動態(tài)定時任務的幾種方式小結

    SpringBoot提供了多種實現(xiàn)定時任務的方式,包括使用@Scheduled注解、SchedulingConfigurer接口、TaskScheduler接口和Quartz框架,@Scheduled適合簡單的定時任務,文中通過代碼示例介紹的非常詳細,需要的朋友可以參考下
    2024-10-10
  • springboot jackson配置教程

    springboot jackson配置教程

    這篇文章主要介紹了springboot jackson配置教程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10

最新評論