ApplicationContext接口的继承关系

1、ApplicationContext接口的继承关系图



 

 2、通过ApplicationContext加载配置文件的四种方式

@Test
public void testApplicationContext() {
	//1.通过类路径装载配置文件
	//ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
	//classpath:applicationContext.xml 等价于 applicationContext.xml
	ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
	UserBean userBean = (UserBean) context.getBean("userBean");
	//输出:keke
	System.out.println(userBean.getUsername());
	
	//2.通过磁盘路径装载配置文件
	//ApplicationContext context2 = new FileSystemXmlApplicationContext("D:/code/02-tanzhouedu/tz_springmvc/src/applicationContext.xml");
	//file:D:/code/02-tanzhouedu/tz_springmvc/src/applicationContext.xml 等价于
	//D:/code/02-tanzhouedu/tz_springmvc/src/applicationContext.xml
	ApplicationContext context2 = new FileSystemXmlApplicationContext("file:D:/code/02-tanzhouedu/tz_springmvc/src/applicationContext.xml");
	UserBean userBean2 = (UserBean) context2.getBean("userBean");
	//输出:keke
	System.out.println(userBean2.getUsername());
	
	//3.通过WebApplicationContext装载配置文件
	//ApplicationContext context3 = WebApplicationContextUtils
	//       .getWebApplicationContext(event.getServletContext());
	
	//4.通过AnnotationConfigApplicationContext注解装载配置文件
	ApplicationContext context4 = new AnnotationConfigApplicationContext("com.tz.dao");
	UserDao userDao = (UserDao) context4.getBean("userDao");
}

猜你喜欢

转载自lipiaoshui2015.iteye.com/blog/2264981