spring学习XmlBeanFactory显示已废弃(Spring启动的多种方式)(使用main方法启动Spring)

看spring源码解析的时候照着例子打发现这个类XmlBeanFactory已经被废弃了。

使用XmlBeanFactory报了一个横线

在这里插入图片描述

方法1 组合的方式

public static void main(String[] args) {
		Resource resource = new ClassPathResource("bean.xml");
		BeanFactory factory = new DefaultListableBeanFactory();
		BeanDefinitionReader bdr = new XmlBeanDefinitionReader((BeanDefinitionRegistry) factory);
		bdr.loadBeanDefinitions(resource);
		Student student = factory.getBean("stu", Student.class);
		System.out.printf("。。。。。。" + student);
	}

方法2 ClassPathXmlApplicationContext

ApplicationContext sc = new ClassPathXmlApplicationContext("bean.xml");
		Student student = sc.getBean("stu", Student.class);
		System.out.printf("。。。。。。" + student);

方法3 FileSystemXmlApplicationContext

 ApplicationContext factory=new FileSystemXmlApplicationContext("bean.xml"); 

方法4 WebApplicationContextUtils获取(web项目使用)

ServletContext servletContext = request.getSession().getServletContext(); 
    ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext ); 
发布了188 篇原创文章 · 获赞 34 · 访问量 15万+

猜你喜欢

转载自blog.csdn.net/u010316188/article/details/98891028
今日推荐