赞
踩
目标:从代码中获取到配置文件中的值
# 配置文件中配置值
SYSTEM_ENV=local
# 在有 @Controller 或者 @Service注解的类中使用
@Value("${SYSTEM_ENV}")
private String env;
# --------------------------------------
# 取值
System.out.printf(env);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
# 配置文件中配置值 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());
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
# @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); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
在配置文件中,用上面的类,代替 PropertyPlaceholderConfigurer
<bean id="propertyConfigurer" class="com.mybatis.util.PropertyPlaceholder">
<property name="location">
<value>classpath:config.properties</value>
</property>
</bean>
这样在代码中就可以直接用编程方式获取
PropertyPlaceholder.getProperty("SYSTEM_ENV");
- 1
<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>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。