yin.kh

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

不通过注解或者是配置文件怎么获取spring中定义的bean呢?有几个方法:

1、实现ApplicationContextAware 

<bean class="com.xxx.SpringUtil"/>

SpringUtil内容如下:

 

public class SpringUtil implements ApplicationContextAware {

private static ApplicationContext ac;

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ac = applicationContext;
}

public static <T> T getBean(String beanName, Class<T> clazz) {
return ac.getBean(beanName, clazz);
}

}

 

启动的时候初始化这个类,由于这个类全部是静态方法,所以可以在项目中随便用。

posted on 2016-05-16 21:34  yin.kh  阅读(181)  评论(0编辑  收藏  举报