Last active
August 29, 2015 14:01
-
-
Save yanhua365/66660272c68b8831e2a6 to your computer and use it in GitHub Desktop.
访问Spring中placeholder里的属性值(也可以是任何合法的SpringEL表达式)的方法。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cn.boxiao.bxn.common.boot; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.support.AbstractBeanFactory; | |
import org.springframework.stereotype.Component; | |
import java.util.Map; | |
import java.util.concurrent.ConcurrentHashMap; | |
/** | |
* 获取属性文件的值,利用了spring的表达式功能 | |
*/ | |
@Component | |
public class PropertiesAccessor { | |
private Logger logger = LoggerFactory.getLogger(getClass()); | |
private final AbstractBeanFactory beanFactory; | |
private final Map<String,String> cache = new ConcurrentHashMap<>(); | |
@Autowired | |
protected PropertiesAccessor(@SuppressWarnings("SpringJavaAutowiringInspection") AbstractBeanFactory beanFactory) { | |
this.beanFactory = beanFactory; | |
} | |
/** | |
* 得到一个key对应的值 | |
* 如果key在properties文件中配置,那么返回null | |
* @param key | |
* @return | |
*/ | |
public String getProperty(String key) { | |
if(cache.containsKey(key)){ | |
return cache.get(key); | |
} | |
String foundProp = null; | |
try { | |
foundProp = beanFactory.resolveEmbeddedValue("${" + key.trim() + "}"); | |
cache.put(key,foundProp); | |
} catch (IllegalArgumentException ex) { | |
logger.error("Can't get property[{}]'s value,has config it in properties file?", key); | |
} | |
return foundProp; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cn.boxiao.bxn.common.util; | |
import cn.boxiao.bxn.common.boot.PropertiesAccessor; | |
import org.springframework.web.context.WebApplicationContext; | |
import org.springframework.web.context.request.RequestContextHolder; | |
import org.springframework.web.context.request.ServletRequestAttributes; | |
import org.springframework.web.servlet.support.RequestContextUtils; | |
import javax.servlet.http.HttpServletRequest; | |
/** | |
* 对{@link cn.boxiao.bxn.common.boot.PropertiesAccessor}的方法做了静态方法的包装 | |
* 常用于不使用Spring注入的对象使用(比如VO) | |
* 但要特别注意,此方法不能脱离Spring的环境而使用,且必须配置{@link org.springframework.web.context.request.RequestContextListener} | |
* Created by yanhua on 14-5-8. | |
*/ | |
public class PropertiesAccessorUtil { | |
private static PropertiesAccessor propertiesAccessor; | |
/** | |
* 对{@link cn.boxiao.bxn.common.boot.PropertiesAccessor#getProperty(String)}的封装 | |
* @param key | |
* @return | |
*/ | |
public static String getProperty(String key){ | |
if(propertiesAccessor == null){ | |
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(); | |
WebApplicationContext applicationContext = RequestContextUtils.getWebApplicationContext(request); | |
propertiesAccessor = applicationContext.getBean(PropertiesAccessor.class); | |
} | |
return propertiesAccessor.getProperty(key); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment