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