A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© yangcy 中级黑马   /  2014-7-12 13:56  /  1097 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

web开发中,一般采用spring框架来管理DAO层和Service层中的bean,当需要使用这些bean的时候,spring会利用其IOC来帮我们注入这些bean。然后,我们可以直接拿着bean狂用。但是如果需要在不是由spring管理的java类中使用这些bean怎么办,经过参考了一些资料后,整理出一个获取bean的工具类。
获取由spring管理的bean的工具类如下:
  1. import org.springframework.beans.BeansException;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.ApplicationContextAware;

  4. public class GetBeanUtil implements ApplicationContextAware {
  5.         private static ApplicationContext applicationContext;

  6.         private GetBeanUtil() {
  7.                 super();
  8.         }

  9.         public void setApplicationContext(ApplicationContext arg0)
  10.                         throws BeansException {
  11.                 applicationContext = arg0;
  12.         }

  13.         public static Object getBean(String paramString) {
  14.                 return applicationContext.getBean(paramString);
  15.         }
  16. }
复制代码


评分

参与人数 1技术分 +1 收起 理由
HM汪磊 + 1 很给力!

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马