如图,我想写一个关于 环境的工具类。(dev/stage/pro) 。
已知:spring中@Autowired Environment env注解可以注入当前环境。
所以ApplicationContext中存在env的bean。
,EnvironmentHelper
类里实现 获取dev. yml,pro. yml配置 的 信static息方法
思路是:A我导pplicationContext中Enviroment envm
但结果显示 似在静态方法不能使env的javabean用。
这种问题怎么解决备注:ContextUtils自定义的工具类,获取ApplicationContext容器。?
已知:spring中@Autowired Environment env注解可以注入当前环境。
所以ApplicationContext中存在env的bean。
,EnvironmentHelper
类里实现 获取dev. yml,pro. yml配置 的 信static息方法
思路是:A我导pplicationContext中Enviroment envm
但结果显示 似在静态方法不能使env的javabean用。
这种问题怎么解决备注:ContextUtils自定义的工具类,获取ApplicationContext容器。?
解决方案 »
- 今天去面试,碰到一个笔试题,谁帮我翻译下?
- Servlet的原理和生命周期
- VML画地图
- [求助] Hibernate 左联接问题
- jsp简单问题
- 奇了怪了。我的JSP页面全是静态代码,为什么会显示出空白页,在网上找了N多种症状都对不上我的问题啊。。。。
- javamail 如何接收html(复合邮件)?(有好的方案我可以在加分,我还有好几千分呢)
- <JUnit In Action>,Who can tell me ,how to get one? thanks!
- jbuilder7+weblogic7开发cmp,其中ejb-ql语言的where条件好象不能用等号,一用就出现如下问题
- 求救,win2000j2ee的配置问题
- 关于springboot 打包jar ,运行找不到静态资源,求指教,巨额分数奉上
- 对于对象.getClass和对象名.class的区别
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;/**
* SpringUtils.java
* @author songb
*/
@Component
public class SpringUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext; @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if(SpringUtils.applicationContext == null) {
SpringUtils.applicationContext = applicationContext;
}
} //获取applicationContext
public static ApplicationContext getApplicationContext() {
return applicationContext;
} //通过name获取 Bean.
public static Object getBean(String name){
return getApplicationContext().getBean(name);
} //通过class获取Bean.
public static <T> T getBean(Class<T> clazz){
return getApplicationContext().getBean(clazz);
} //通过name,以及Clazz返回指定的Bean
public static <T> T getBean(String name,Class<T> clazz){
return getApplicationContext().getBean(name, clazz);
}
}