熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

使用SpringtUtil獲取Spring IoC容器中的Bean

2013-11-23 20:19:21  來源: Java開源技術 
功能
    方便獲取Spring IoC容器中的Bean調用方法getBean(String name)

  import orgntextApplicationContext;

  import orgntextsupportClassPathXmlApplicationContext;

  import orgntextsupportFileSystemXmlApplicationContext;

  public class SpringtUtil {

  private static SpringtUtil instance = new SpringtUtil();

  private static ApplicationContext context;

  public static final String spring_context_configfile = nfigfile;

  public static Object getBean(String name) {

  return getContext()getBean(name);

  }

  public static ApplicationContext getContext() {

  return SpringtUtilgetInstance(ntext;

  }

  public  static SpringtUtil getInstance()

  {

  return instance;

  }

  private SpringtUtil()

  {

  try

  {

  String springContextFileName =PropertiesUtilgetProperty(spring_context_configfile);

  //Systemoutprintln(springContextFileName);

  context = new FileSystemXmlApplicationContext(springContextFileName);

  } catch (Throwable t) {

  tprintStackTrace();

  }

  }

  public static void main(String[] args)

  {

  try {

  UserService userService= (UserService)SpringtUtilgetBean(userService);

  userServicetest(id name);

  } catch (Exception e) {

  eprintStackTrace();

  }

  }

  }


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28304.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.