Spring注解@Value取值

目标:从代码中获取到配置文件中的值


方法一、

# 配置文件中配置值
SYSTEM_ENV=local

# 在有 @Controller 或者 @Service注解的类中使用
@Value("${SYSTEM_ENV}")
private String env;

# --------------------------------------
# 取值
System.out.printf(env);

方法二、

# 配置文件中配置值
SYSTEM_ENV=local

# 赋值方式
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author ryiann
 * @version $Id SystemInfo.java, v 0.1 2018-08-26 12:51 ryiann Exp $
 */
@Component
public class SystemInfo {

    @Value("${SYSTEM_ENV}")
    private String env;

    public String getEnv() {
        return env;
    }
    
}

# -------------------------------------------------------
# 取值(用 @Autowired@Autowired
private SystemInfo systemInfo;
System.out.printf(systemInfo.getEnv());

方法三、

# @Value("#{}")表示SpEl表达式通常用来获取bean的属性,或者调用bean的某个方法
# 配置文件中配置
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="locations">
        <list>
            <value>classpath:config.properties</value>
        </list>
    </property>
</bean>

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
    <property name="properties" ref="configProperties" />
</bean>

# ------------------------------------------------------------------------------------------------
# 取值

@Value("#{configProperties['SYSTEM_ENV']}") 
private String env;

方法四、

每用一次配置文件中的值,就要声明一个局部变量,有没有用代码的方式,直接读取配置文件中的值?

答案就是重写 PropertyPlaceholderConfigurer

/**
 * Ryana.cn Inc.
 * Copyright (c) 2018-2018 All Rights Reserved.
 */
package com.mybatis.util;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author ryiann
 * @version $Id PropertyPlaceholder.java, v 0.1 2018-08-26 14:20 ryiann Exp $
 */
public class PropertyPlaceholder extends PropertyPlaceholderConfigurer {

    private static Map<String,String> propertyMap;

    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
        super.processProperties(beanFactoryToProcess, props);
        propertyMap = new HashMap<String, String>();
        for (Object key : props.keySet()) {
            String keyStr = key.toString();
            String value = props.getProperty(keyStr);
            propertyMap.put(keyStr, value);
        }
    }

    //static method for accessing context properties
    public static Object getProperty(String name) {
        return propertyMap.get(name);
    }
    
}

在配置文件中,用上面的类,代替 PropertyPlaceholderConfigurer

<bean id="propertyConfigurer" class="com.mybatis.util.PropertyPlaceholder">
    <property name="location">
        <value>classpath:config.properties</value>
    </property>
</bean>

这样在代码中就可以直接用编程方式获取

PropertyPlaceholder.getProperty("SYSTEM_ENV");
<bean id="propertyConfigurer" class="com.gyoung.mybatis.util.PropertyPlaceholder">
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="locations">
        <list>
            <value>classpath:jdbc.properties</value>
            <value>classpath:config.properties</value>
        </list>
    </property>
</bean>

@value 取不到值的几种情况

  • spring组件重写构造方法,在构造方法中引用@value为null
  • 调用spring组件时使用new对象,而不是@Autowired
  • 使用final或static修饰成员变量
  • spring mvc中引用@value为null

参考地址:https://www.cnblogs.com/Gyoung/p/5507063.html

猜你喜欢

转载自blog.csdn.net/yori_chen/article/details/82793717