手动从spring WebApplicationContext 获取bean (一)

来源:互联网 发布:电子地图标注软件破解 编辑:程序博客网 时间:2024/06/07 06:25
import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;/** * 手动从Spring WebApplicationContext 容器中获得bean */public class BeanUtils implements ApplicationContextAware {  private static ApplicationContext applicationContext;  public static Object getBean(String beanName) {  return applicationContext.getBean(beanName); }  public static <T> T getBean(String beanName, Class<T> clazs) {  return clazs.cast(getBean(beanName)); } public void setApplicationContext(ApplicationContext applicationContext)   throws BeansException {  BeanUtils.applicationContext = applicationContext; } }
配置文件<bean id="beanUtils" class="cn.xxx.util.BeanUtils" />

 

原创粉丝点击