ApplicationContext
上图表示了ApplicationContext的依赖关系:
(1)BeanFactory
是Spring容器的依赖注入的基础,位于类结构树的项端,接口中最重要的方法就是getBean(String),得到特定名称的Bean对象。
(2)ListableBeanFactory
根据名称可以看出,相当于对一个容器中的内容相关信息的获取。
(3)HierachicalBeanFactory
容器的层次化,获取父级BeanFactory,但是ApplicationContext接口中没有setParent()方法。
(4)ResourceLoader
资源加载接口,用于对不同的Resource进行加载,通过标识加载资源信息。
(5)ResourcePatternResolver
是的ResourceLoader的扩展,将一个标识拆分成多个资源。
(6)MessageSource
提供消息处理的功能,在web项目上用于国际化。
(7)EnvironmentCapable
运行环境接口,通过这个接口可以获取Environement。
(8)ApplicationEventPublisher
提供了发布event组件的接口。
ApplicationContext主要实现类
1.ConfigurableApplicationContext
它内部提供了set方法:
(1)setId对应ApplicationContext中的get方法
(2)setParent对应HierachicalBeanFactory中的get方法
(3)setEnvironment对应EnvironmentCapable中的方法。
它的addApplicationListener对应ApplicationEventPublisher,扩展之后可以发布事件,也有了监听能力。
它的addBeanFactoryProcessor为操作容器内的bean提供了接口。
2.AbstractApplicationContext
(1)它定义了一些常量
MESSAGE_SOURCE_BEAN_NAME:统一的信息资源名称,messageSourceLIFECYCLE_PROCESSOR_BEAN_NAME:统一的生命周期处理工具Bean的名称 ,lifecycleProcessorAPPLLICATION_EVENT_MULTICASTER_BEAN_NAME:上下文事件的多路广播名称,applicationEventMulticaster。
(2)定义了一些Properties
(3)同时还实现了DisposableBean接口,提供了对单例的bean进行销毁的操作方法。
3.AbstractRefreshableApplicationContext
(1)实现了AbstractApplicationContext的部分抽象方法。
实现了refreshBeanFactory方法,完成对容器底层的beanFactory的刷新。
(2)扩展了一些Properties
4.AbstractRefreshConfigApplicationContext
不仅对AbstractRefreshableApplicationContext进行扩展,同时继承了BeanNameAware和InitializingBean。
(1)定义了一个setIdCalled属性,用于判断是否调用过setId方法。
(2)继承了BeanNameAware,实现了setBeanName方法,这个意思是将context本身当做一个bean,设置一个beanName。但是在设置这个beanName有一个条件就是没有调用过setId方法。
(3)继承了InitializingBean,实现了afterPropertiesSet方法,方法的意思是定义初始化方法的方式。如果不是active的话就执行refresh()方法。
5.AbstractXmlApplicationContext
(1)定义了一个validating属性,默认为true
(2)实现了loadBeanDefinition方法,通过XmlBeanDefinitionReader加载bean的定义
6.ClassPathXmlApplicationContext
实现了AbstractXmlApplicationContext的抽象方法。
7.FileSystemXmlApplicationContext
和ClassPathXmlApplicationiContext相比就是加载configLocations的方法不一样。一个是classPath路径,一个是文件系统路径 。
8.AnnotationConfigApplicationContext
<beans> <bean id="course" class="demo.Course"> <property name="module" ref="module"/> </bean> <bean id="module" class="demo.Module"> <property name="assignment" ref="assignment"/> </bean> <bean id="assignment" class="demo.Assignment" /> </beans>以上的XML就是你使用Spring配置bean时通常会编写的代码,现在要删除这段XML,编写同等效果的Java代码。
@Configuration public class AppContext { @Bean public Course course() { Course course = new Course(); course.setModule(module()); return course; } @Bean public Module module() { Module module = new Module(); module.setAssignment(assignment()); return module; } @Bean public Assignment assignment() { return new Assignment(); } }
AnonotationConfigApplicationContext类是ApplicationContext接口的一个实现,使你能够注册所注解的配置类。
配置类使用@Configuration注解,注册的类使用@Bean注解。
public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(AppContext.class); Course course = ctx.getBean(Course.class); course.getName(); }
<web-app> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet> <servlet-name>sampleServlet</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> </servlet> ... </web-app>
通常使用XmlWebApplicationContext上下文来配置Spring Web应用程序,XmlWebApplicationContext是web应用程序 使用的的默认上下文类。
<web-app> <context-param> <param-name>contextClass</param-name> <param-value> org.springframework.web.context. support.AnnotationConfigWebApplicationContext </param-value> </context-param> <context-param> <param-name>contextConfigLocation</param-name> <param-value> demo.AppContext </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet> <servlet-name>sampleServlet</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextClass</param-name> <param-value> org.springframework.web.context. support.AnnotationConfigWebApplicationContext </param-value> </init-param> </servlet> ... </web-app>
修改后web.xml现在定义了AnnotationConfigWebApplicationContext上下文类,并将其作为上下文参数和servlet元素的一部分。
9.WebApplicationContext
专门为web应用准备,它允许从相对于web根目录的路径中加载配置文件完成初始化工作。它扩展了ApplicationContext,定义了一个常量ROOT_WEB_APPLICATION_CONTEXTL_ATTRIBUTE,通过下面语句可以获取WebApplicationContext.
WebApplicationContext wac=(WebApplicationContext)servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
10.ConfigurableWebApplicationContext
扩展了WebApplicationContext,它允许通过配置的方式实例化WebApplicationContext
setServletContext(ServletContext servletContext):为Spring设置Web应用上下文,以便两者整合。 setConfigLocation(String[] configLocations)设置Spring配置文件地址,一般情况下,配置文件地址是相对于Web根目录的地址,如/WEB-INF/techman-dao.xml等。也可以使用classpath:com/techman/context/techman-dao.xml等格式。
最后欢迎大家访问我的个人网站:1024s