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 $
resource和autowired注解的区别*/
@Component
public class SystemInfo {
@Value("${SYSTEM_ENV}")
private String env;
public String getEnv(){
return env;
}
}
# -------------------------------------------------------
# 取值(⽤@Autowired)
@Autowired
private SystemInfo systemInfo;
System.out.Env());
⽅法三、
# @Value("#{}")表⽰SpEl表达式通常⽤来获取bean的属性,或者调⽤bean的某个⽅法
# 配置⽂件中配置
<bean id="configProperties"class="org.springframework.fig.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:config.properties</value>
</list>
</property>
</bean>
<bean id="propertyConfigurer"class="org.springframework.fig.PreferencesPlaceholderConfigurer">
<property name="properties"ref="configProperties"/>
</bean>
# ------------------------------------------------------------------------------------------------
# 取值
@Value("#{configProperties['SYSTEM_ENV']}")
private String env;
⽅法四、
每⽤⼀次配置⽂件中的值,就要声明⼀个局部变量,有没有⽤代码的⽅式,直接读取配置⽂件中的值?
答案就是重写 PropertyPlaceholderConfigurer
/**
* Ryana Inc.
* Copyright (c) 2018-2018 All Rights Reserved.
*/
batis.util;
import org.springframework.beans.BeansException;
import org.springframework.fig.ConfigurableListableBeanFactory;
import org.springframework.fig.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 = String();
String value = Property(keyStr);
propertyMap.put(keyStr, value);
}
}
//static method for accessing context properties
public static Object getProperty(String name){
(name);
}
}
在配置⽂件中,⽤上⾯的类,代替 PropertyPlaceholderConfigurer
<bean id="propertyConfigurer"class="batis.util.PropertyPlaceholder">
<property name="location">
<value>classpath:config.properties</value>
</property>
</bean>
这样在代码中就可以直接⽤编程⽅式获取
<bean id="propertyConfigurer"class="batis.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
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论