1. 概述

在 Spring 组件中使用 @Value 注解的方式,很方便的读取 properties 文件的配置值。

2.使用场景

声明的变量中使用。

public static class FieldValueTestBean {@Value("#{ systemProperties['user.region'] }")private String defaultLocale;}

setter 方法中。

public static class PropertyValueTestBean {private String defaultLocale;@Value("#{ systemProperties['user.region'] }")public void setDefaultLocale(String defaultLocale) {this.defaultLocale = defaultLocale;}}

方法。

public class SimpleMovieLister {private MovieFinder movieFinder;private String defaultLocale;@Autowiredpublic void configure(MovieFinder movieFinder,@Value("#{ systemProperties['user.region'] }") String defaultLocale) {this.movieFinder = movieFinder;this.defaultLocale = defaultLocale;}// ...
}

构造方法。

public class MovieRecommender {private String defaultLocale;private CustomerPreferenceDao customerPreferenceDao;@Autowiredpublic MovieRecommender(CustomerPreferenceDao customerPreferenceDao,@Value("#{systemProperties['user.country']}") String defaultLocale) {this.customerPreferenceDao = customerPreferenceDao;this.defaultLocale = defaultLocale;}// ...
}

3. 字符串

字符串类型的属性设置默认值。

@Value("${some.key:my default value}")
private String stringWithDefaultValue;

some.key 没有设置值,stringWithDefaultValue 变量值将会被设置成 my default value 。

如果默认值设为空,也将会被设置成默认值。

@Value("${some.key:}")
private String stringWithBlankDefaultValue;

4. 基本类型

基本类型设置默认值。

@Value("${some.key:true}")
private boolean booleanWithDefaultValue;@Value("${some.key:42}")
private int intWithDefaultValue;

包装类型设置默认值。

@Value("${some.key:true}")
private Boolean booleanWithDefaultValue;@Value("${some.key:42}")
private Integer intWithDefaultValue;

5. 数组

数组的默认值可以使用逗号分割。

@Value("${some.key:one,two,three}")
private String[] stringArrayWithDefaults;@Value("${some.key:1,2,3}")
private int[] intArrayWithDefaults;

6. SpEL

使用 Spring Expression Language (SpEL) 设置默认值。

下面的代码标示在systemProperties属性文件中,如果没有设置 some.key 的值,my default system property value 会被设置成默认值。

@Value("#{systemProperties['some.key'] ?: 'my default system property value'}")
private String spelWithDefaultValue;

7.结语

上面讲解使用 Spring @Value 为属性设置默认值。在项目中,提供合理的默认值,在大多情况下不用任何配置,就能直接使用。达到零配置的效果,降低被人使用的门槛。简化新Spring应用的搭建、开发、部署过程。

8.参考链接

Using Spring @Value with Defaults

Annotation-based configuration

Spring @Value 用法相关推荐

  1. Spring AOP用法

    软件152 杨浩艺 Spring AOP Java web 环境搭建 Java web 项目搭建 Java Spring IOC用法 spring提供了两个核心功能,一个是IoC(控制反转),另外一个 ...

  2. Java Spring IOC用法

    Java Spring IOC用法 Spring IoC 转载于:http://www.cnblogs.com/flowwind/p/4772375.html 在前两篇文章中,我们讲了java web ...

  3. spring RestTemplate用法详解

    spring RestTemplate用法详解 spring 3.2.3 框架参考有说明 21.9 Accessing RESTful services on the Client 转载于:https ...

  4. Spring @Autowired 用法

    Spring @Autowired 用法 首先看下@Component ```举例 1 :``` ```举例 2 :``` 验证是否调用的是默认构造器 ```如何,在启动的时候执行有参数的构造函数?? ...

  5. Spring基本用法1——Spring的核心机制:IOC、DI

            前言:一直想写一个关于Spring的系列文章,但是构思许久却不知道以什么方式阐述,毕竟要把一个复杂框架说清楚并不是那么容易的,我也只能尽力而为了.Spring系列文章打算以这样的顺序展 ...

  6. Spring Boot spring.factories 用法及原理

    文章目录 1. spring.factories 用法 2. spring.factories 实现原理 3. spring.factories 用于解决什么问题? 3.1 业务场景思考及 start ...

  7. spring @Autowired用法

    首先要知道另一个东西,default-autowire,它是在xml文件中进行配置的,可以设置为 byName.byType.constructor 和 autodetect. 比如byName,不用 ...

  8. spring@PropertySource用法

    测试例子 packagecom.hjzgg.auth.config;importorg.springframework.beans.factory.annotation.Autowired;impor ...

  9. Spring 事务用法示例与实现原理

    关于事务,简单来说,就是为了保证数据完整性而存在的一种工具,其主要有四大特性:原子性,一致性,隔离性和持久性.对于Spring事务,其最终还是在数据库层面实现的,而Spring只是以一种比较优雅的方式 ...

  10. Spring --getBean用法

    1.在初始化时保存ApplicationContext对象 2.通过Spring提供的utils类获取ApplicationContext对象 3.继承自抽象类ApplicationObjectSup ...

最新文章

  1. mysql开发规范最新版本_MYSQL 开发规范
  2. 传潘石屹投资爱蜂潮 天猫不予评论
  3. Python安装时我遇到的一些问题
  4. leetcode 【 Unique Paths 】python 实现
  5. 如何查询一个进程下面的线程数(进程和线程区别)
  6. pythonturtle画点的指令_简述python的turtle绘画命令及解释
  7. aspnet_regsql
  8. Linq语言集成查询
  9. 算法:位运算加减乘除
  10. centos 7 单独安装mysql和mysqli和pdo_mysql扩展
  11. PeopleTools 8.54 first install note
  12. 软件测试——测试计划
  13. NV12转BGR24算法总结
  14. 程序员背锅救星-docker
  15. ApacheCN 活动汇总 2019.8.9
  16. 【AutoHotkey】笔记本键盘没有Home键和End键的解决方案
  17. CSS盒模型的应用--个人名片的制作
  18. A micro Lie theory for state estimation in robotics005:The tangent spaces and the Lie algebra
  19. 外国人入境日本 后天起须留指印头像
  20. postman进行批量测试的步骤

热门文章

  1. 爬取新型冠状病毒的历史数据
  2. 微信小程序对接串口摄像头
  3. 计算机处理器i3 i5,i3与i5以及i7处理器有什么区别?
  4. Java导入Excel文档到数据库
  5. ZYNQ-AXI DMA IP简介
  6. 一文了解滴滴与蚂蚁金服开源共建的SQLFlow
  7. Python--所有的库
  8. Flutter生命周期
  9. 还敢抱怨么?30多万没了吧……顶、
  10. 02网络爬虫-使用 Beautiful Soup 解析网页