zoukankan      html  css  js  c++  java
  • Spring @Value 设置默认值

    本文转载自Spring @Value 设置默认值

    概述

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

    使用场景

    声明的变量中使用。

    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;
    
        @Autowired
        public 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;
    
        @Autowired
        public MovieRecommender(CustomerPreferenceDao customerPreferenceDao,
                @Value("#{systemProperties['user.country']}") String defaultLocale) {
            this.customerPreferenceDao = customerPreferenceDao;
            this.defaultLocale = defaultLocale;
        }
    
        // ...
    }
    

    字符串

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

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

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

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

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

    基本类型

    基本类型设置默认值。

    @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;
    

    数组

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

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

    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;
    

    结语

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

    参考链接

    Using Spring @Value with Defaults

    Annotation-based configuration

  • 相关阅读:
    xls10-Python3安装cx_Oracle连接oracle数据库实操总结list
    xls3-2-想要使用Python的xlwt设置单元格的背景色
    XLS9-PyCharm下打包*.py程序成.exe
    XLS8-python3+PyQt5+pycharm桌面GUI开发
    epoll模型中LT、ET模式分析
    lambda函数也叫匿名函数,即,函数没有具体的名称。先来看一个最简单例子:
    xls7-python读conf配置文件--ConfigParser
    xls6-python解析properties文件
    xls5-解析properties文件,在python中基本没有遇到
    xls2- 用Python读写Excel文件-乘法口诀
  • 原文地址:https://www.cnblogs.com/yungyu16/p/13256066.html
Copyright © 2011-2022 走看看