当前位置:首页 > 开发 > 编程语言 > Spring > 正文

我的spring学习笔记5-如何使用ApplicationContext替换BeanFactory

发表于: 2012-04-12   作者:aijuans   来源:转载   浏览:
摘要: 如何使用ApplicationContext替换BeanFactory? package onlyfun.caterpillar.device; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import
如何使用ApplicationContext替换BeanFactory?
package onlyfun.caterpillar.device;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class SpringDemo {
public static void main(String[] args) {
   Resource rs = new ClassPathResource("beans-config.xml");
   BeanFactory factory = new XmlBeanFactory(rs);
  
   Business business = (Business) factory.getBean("business");
   Business business4Usb = (Business) factory.getBean("business4Usb");  
   business.save();
   business4Usb.save();
}
}

上面是用BeanFactory的方式。
package onlyfun.caterpillar.device;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringDemo2 {
public static void main(String[] args) {
   ApplicationContext context = new ClassPathXmlApplicationContext("beans-config.xml");
   ((Business) context.getBean("business")).save();
   ((Business) context.getBean("business4Usb")).save();  
}
}

这个是用ApplicationContext的方式。

我的spring学习笔记5-如何使用ApplicationContext替换BeanFactory

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号