• ApplicationContext是BeanFactory的子接口。
• BeanFactory通常称为Bean容器,ApplicationContext通常称为应用上下文。
• ApplicationContext的功能要多于BeanFactory,如它可以加载外部的资源文件、可以自动进行AOP切面、可以识别自动代理的的类、可以识别用于监听Bean创建的类等。
在实体使用时,应使用ApplicationContext类型的对象。
ApplicationContext是一个接口,有多个实现类,我们在使用时就是用的他的某个实现类:
1, ClassPathXmlApplicationContext
2, FileSystemXmlApplicationContext
3, XmlWebApplicationContext(在Web应用中使用)
ClassPathXmlApplicationContext(读取classpath中的资源)
@Test
publicvoid testClassPathXmlApplicationContext_1() {
// 指定一个配置文件,需要指定一个相对于classpath根目录的相对路径
ApplicationContext applicationContext = newClassPathXmlApplicationContext(//
"cn/itcast/c_applicationcontextSubclass/applicationContext_dao.xml");
String[] names = applicationContext.getBeanDefinitionNames();// 获取所有定义的bean的名称
System.out.println(Arrays.toString(names));
}
@Test
publicvoid testClassPathXmlApplicationContext_2() {
// 指定多个配置文件,需要指定多个相对于classpath根目录的相对路径
ApplicationContext applicationContext = newClassPathXmlApplicationContext(//
new String[] {
"cn/itcast/c_applicationcontextSubclass/applicationContext_dao.xml",
"cn/itcast/c_applicationcontextSubclass/applicationContext_service.xml" }
);
String[] names =applicationContext.getBeanDefinitionNames(); // 获取所有定义的bean的名称
System.out.println(Arrays.toString(names));
}
@Test
publicvoid testClassPathXmlApplicationContext_3() {
// 指定一个配置文件,需要指定一个相对于某类的相对路径
// 第2个参数是表示相对于这个类的路径
ApplicationContextapplicationContext = newClassPathXmlApplicationContext(//
"applicationContext_dao.xml",//
MainTest.class);
String[] names =applicationContext.getBeanDefinitionNames(); // 获取所有定义的bean的名称
System.out.println(Arrays.toString(names));
}
@Test
publicvoid testClassPathXmlApplicationContext_4() {
// 指定多个配置文件,需要指定多个相对于某类的相对路径
// 第2个参数是表示相对于这个类的路径
ApplicationContextapplicationContext = newClassPathXmlApplicationContext(//
new String[] { "applicationContext_dao.xml",
"applicationContext_service.xml" },
MainTest.class
);
String[] names =applicationContext.getBeanDefinitionNames(); // 获取所有定义的bean的名称
System.out.println(Arrays.toString(names));
}
FileSystemXmlApplicationContext(读取指定路径的资源)
@Test
publicvoid testFileSystemXmlApplicationContext_1() {
// 指定一个配置文件,推荐写绝对路径
ApplicationContext ac = new FileSystemXmlApplicationContext(
"applicationContext_dao.xml");
String[] names = ac.getBeanDefinitionNames();
System.out.println(Arrays.toString(names));
}
@Test
publicvoid testFileSystemXmlApplicationContext_2() {
// 指定多个配置文件,推荐写绝对路径
ApplicationContext ac = new FileSystemXmlApplicationContext(//
new String[] { "c:/applicationContext_dao.xml",//
"c:/applicationContext_service.xml"//
});
String[] names = ac.getBeanDefinitionNames();
System.out.println(Arrays.toString(names));
}
XmlWebApplicationContext
// 注意:需要在Web的环境下才可以运行
@Test
publicvoid testXmlWebApplicationContext(){
XmlWebApplicationContext ac = new XmlWebApplicationContext(); // 这时并没有初始化容器
ac.setServletContext(servletContext);// 需要指定ServletContext对象
ac.setConfigLocation("/WEB-INF/applicationContext.xml"); // 指定配置文件路径,开头的斜线表示Web应用的根目录
ac.refresh(); // 初始化容器
String[] names = ac.getBeanDefinitionNames(); // 获取所有定义的bean的名称
System.out.println(Arrays.toString(names));
Resource(Spring中的资源定义)
此接口的全名为: org.springframework.core.io.Resource
比较常用的资源定义的实现类为:
1, ClassPathResource 从classpath中读取
2, FileSystemResource 从文件系统中读取
3, UrlResource 从指定URL中读取
4, ServletContextResource 必须要在web环境下使用
ClassPathResource
@Test
publicvoid testClassPathResource_1() throws Exception {
// 指定一个相对于classpath根目录的相对路径
Resource resource = new ClassPathResource(//
"cn/itcast/d_resource/applicationContext.xml");
System.out.println(resource.getFile().getAbsolutePath());
}
@Test
publicvoid testClassPathResource_2() throws Exception {
// 指定一个相对于指定类的相对路径
Resource resource = new ClassPathResource("applicationContext.xml", MainTest.class);
System.out.println(resource.getFile().getAbsolutePath());
}
FileSystemResource
@Test
publicvoid testFileSystemResource_1() throws Exception {
// 指定一个资源路径,推荐写绝对路径
Resource resource = new FileSystemResource("c:/applicationContext.xml");
System.out.println(resource.getFile().getAbsolutePath());
}
UrlResource
@Test
publicvoid testUrlResource() throws Exception {
// 指定一个URL,如file://...或是http://...等等
Resource resource = new UrlResource("file://c:/applicationContext.xml");
System.out.println(resource.getFile().getAbsolutePath());
}
ServletContextResource
// 注:要在Web环境中使用
@Test
publicvoid testServletContextResource(){
// 路径开头的斜线代表当前Web应用的根目录
String path = "/WEB-INF/applicationContext.xml";
Resource resource = new ServletContextResource(servletContext, path);
System.out.println(resource.getFile().getAbsolutePath());
}
使用特定格式的字符串表示各种类型的Resource
@Test
publicvoid testResourceString() throws Exception {
ApplicationContext ac = new ClassPathXmlApplicationContext(//
"applicationContext.xml", getClass());
// ClassPathResource
Resource resource1 = ac.getResource(//
"classpath:cn/itcast/d_resource/applicationContext.xml");
// UrlResource
Resource resource2 = ac.getResource("file://c:/applicationContext.xml");
// UrlResource
Resource resource3 = ac.getResource("http://www.itcast.cn/applicationContext.xml");
System.out.println(resource1.getFile().getAbsolutePath());
System.out.println(resource2.getFile().getAbsolutePath());
System.out.println(resource3.getFile().getAbsolutePath());
}