栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring4如何自定义@Value功能详解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring4如何自定义@Value功能详解

前言

本文主要给大家介绍了关于Spring4自定义@Value功能的相关内容,使用的Spring版本4.3.10.RELEASE,下面话不多说了,来一起看看详细的介绍吧。

@Value在Spring中,功能非常强大,可以注入一个配置项,可以引用容器中的Bean(调用其方法),也可以做一些简单的运算

如下的一个简单demo,演示@Value的用法

import org.springframework.stereotype.Service; 
 
 
@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; 
  
  
 @Value("#{userService.count()}") 
 private int userCount; 
  
  
 @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(); 
  
  
 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

发现已经实现了@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就做不到,但是,可以用我上面的解决方案。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对考高分网的支持。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/144467.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号