获取spring容器,以访问容器中定义的其他bean
2015-08-17 15:00
666 查看
package com.ihappylife.spring.context;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 获取spring容器,以访问容器中定义的其他bean
*/
public class SpringContextUtil implements ApplicationContextAware {
// Spring应用上下文环境
private static ApplicationContext applicationContext;
/**
* 实现ApplicationContextAware接口的回调方法,设置上下文环境
*/
public void setApplicationContext(ApplicationContext applicationContext)throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 获取对象
* 这里重写了bean方法,起主要作用
* @param name
* @return Object 一个以所给名字注册的bean的实例
* @throws BeansException
*/
public static Object getBean(String beanId) throws BeansException {
if(applicationContext==null){
applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext-*.xml");
}
return applicationContext.getBean(beanId);
}
}
搜索
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 获取spring容器,以访问容器中定义的其他bean
*/
public class SpringContextUtil implements ApplicationContextAware {
// Spring应用上下文环境
private static ApplicationContext applicationContext;
/**
* 实现ApplicationContextAware接口的回调方法,设置上下文环境
*/
public void setApplicationContext(ApplicationContext applicationContext)throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 获取对象
* 这里重写了bean方法,起主要作用
* @param name
* @return Object 一个以所给名字注册的bean的实例
* @throws BeansException
*/
public static Object getBean(String beanId) throws BeansException {
if(applicationContext==null){
applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext-*.xml");
}
return applicationContext.getBean(beanId);
}
}
搜索
相关文章推荐
- java 反射
- JavaBean对象转map
- java代码执行顺序
- java验证码
- eclipse优化之eclipse验证设置,让eclipse跑得更快
- springMVC+json构建restful风格的服务
- Spring事务管理只对出现运行期异常进行回滚
- Java安全结构“沙盒”模型4个组成部分
- spring security 多登陆入口实现方式 以及对spring security认证流程的理解
- “J-Hi”Java开源平台的生力军
- eclipse安装使用fat打jar包
- Java中计算某一天所在周的星期一和星期天的日期函数
- java中生成32位随机ID
- Intellij IDEA创建Java Web工程以及部署
- java 建造者模式
- java 之 单例模式
- Java 多线程(六) synchronized关键字详解
- spring-core依赖jar包
- MyEclipse设置编码
- 实习:java集合应用和sql语句