Spring註解@Value取值
阿新 • • 發佈:2018-11-09
目標:從程式碼中獲取到配置檔案中的值
方法一、
# 配置檔案中配置值
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