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

Spring4如何自定義@Value功能詳解

 更新時間:2017年09月06日 10:48:40   作者:西夏一品堂  
這篇文章主要給大家介紹了關(guān)于Spring4如何自定義@Value功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用spring4具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。

前言

本文主要給大家介紹了關(guān)于Spring4自定義@Value功能的相關(guān)內(nèi)容,使用的Spring版本4.3.10.RELEASE,下面話不多說了,來一起看看詳細(xì)的介紹吧。

@Value在Spring中,功能非常強(qiáng)大,可以注入一個配置項,可以引用容器中的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(); 
 } 
} 

運行,輸出結(jié)果

port : 9090

userCount : 10

max : 10

min : 3

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

那么,能否做到,我給定一個表達(dá)式或者具體的值,它能幫忙計算出表達(dá)式的值呢? 也就是說,實現(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(); 
  
 /** 
  * 解析一個表達(dá)式,獲取一個值 
  * @param beanFactory 
  * @param value 一個固定值或一個表達(dá)式。如果是一個固定值,則直接返回固定值,否則解析一個表達(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); 
  //計算一個具體的值(非表達(dá)式) 
  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的表達(dá)式,我們的需求是,使用方可以直接用一個cron表達(dá)式,也可以支持引用一個配置項(把值配置到配置文件中)

比如說

@Job(cron = "0 0 12 * * ?")
@Job(cron = "${app.job.cron}")

這種情況@Value就做不到,但是,可以用我上面的解決方案。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • spring設(shè)置定時任務(wù)方式(@Scheduled)

    spring設(shè)置定時任務(wù)方式(@Scheduled)

    這篇文章主要介紹了spring設(shè)置定時任務(wù)方式(@Scheduled),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • 使用JAXBContext 設(shè)置xml節(jié)點屬性

    使用JAXBContext 設(shè)置xml節(jié)點屬性

    這篇文章主要介紹了使用JAXBContext 設(shè)置xml節(jié)點屬性的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java設(shè)計模式之裝飾模式詳解

    Java設(shè)計模式之裝飾模式詳解

    這篇文章主要介紹了Java設(shè)計模式中的裝飾者模式,裝飾者模式即Decorator?Pattern,裝飾模式是在不必改變原類文件和使用繼承的情況下,動態(tài)地擴(kuò)展一個對象的功能,裝飾模式又名包裝模式。裝飾器模式以對客戶端透明的方式拓展對象的功能,是繼承關(guān)系的一種替代方案
    2022-07-07
  • 使用FeignClient調(diào)用POST表單Body內(nèi)沒有參數(shù)問題

    使用FeignClient調(diào)用POST表單Body內(nèi)沒有參數(shù)問題

    這篇文章主要介紹了使用FeignClient調(diào)用POST表單Body內(nèi)沒有參數(shù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java網(wǎng)絡(luò)編程UDP協(xié)議發(fā)送接收數(shù)據(jù)

    Java網(wǎng)絡(luò)編程UDP協(xié)議發(fā)送接收數(shù)據(jù)

    這篇文章主要為大家詳細(xì)介紹了Java網(wǎng)絡(luò)編程UDP協(xié)議發(fā)送接收數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • 2023最新版本idea用maven新建web項目(親測不報錯)

    2023最新版本idea用maven新建web項目(親測不報錯)

    這篇文章主要給大家介紹了關(guān)于2023最新版本idea用maven新建web項目,Maven是當(dāng)今Java開發(fā)中主流的依賴管理工具,文中介紹的步驟親測不報錯,需要的朋友可以參考下
    2023-07-07
  • 關(guān)于Spring統(tǒng)一異常處理及說明

    關(guān)于Spring統(tǒng)一異常處理及說明

    這篇文章主要介紹了關(guān)于Spring統(tǒng)一異常處理及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • 詳解Java編程中向量(Vector)的應(yīng)用

    詳解Java編程中向量(Vector)的應(yīng)用

    這篇文章主要介紹了Java中的向量(Vector)的應(yīng)用,Vector也是Java中比較常用的一個類,需要的朋友可以參考下
    2015-10-10
  • MyBatisPlus?大數(shù)據(jù)量查詢慢的問題解決

    MyBatisPlus?大數(shù)據(jù)量查詢慢的問題解決

    本文主要介紹了MyBatis?Plus?解決大數(shù)據(jù)量查詢慢問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • 解決Springboot-application.properties中文亂碼問題

    解決Springboot-application.properties中文亂碼問題

    這篇文章主要介紹了解決Springboot-application.properties中文亂碼問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11

最新評論