当前位置 : 主页 > 手机开发 > harmonyos >

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

来源:互联网 收集:自由互联 发布时间:2023-08-25
如何使用ApplicationContext替换BeanFactory? package onlyfun.caterpillar.device;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.ClassP


如何使用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的方式。


网友评论