获取spring容器中的bean

 

//方式一 
ApplicationContext context = new ClassPathXmlApplicationContext(
        new String[] {"services.xml", "daos.xml"});
// an ApplicationContext is also a BeanFactory (via inheritance)
BeanFactory factory = (BeanFactory) context;
//方式二
ApplicationContext c1 = new FileSystemXmlApplicationContext(new String[] {"beans.xml"});
//方式三
XmlBeanFactory context=new XmlBeanFactory(new ClassPathResource("beans.xml"));
HelloWorld hw = (HelloWorld)context.getBean("helloworld");
  

你可能感兴趣的:(spring,bean)