父子容器的启动, 先启动 父 WebApplicationContext
然后自启动 DispatcherServlet ,初始化(子Spring容器) WebApplicationContext
并将 业务层, 持久层的Spring 容器作为父容器
好处:
1.
允许 展现 层 和 业务层,持久层更好的解耦 。因为展现层的bean定义在子容器中,而业务层和持久层的bean
定义在 父容器中, 子容器可以访问父容器中的bean,而父容器访问不到
子容器中的bean
2. 可以将刷新操作限定在一定范围内,spring2.1新增1功能,在DispacherServlet上下文的
子Spring容器刷新时,并不影响 其他的容器 ;
3. 允许 分步骤初始化不同层次的Spring容器,通过ContextLoaderListener配合使用
contextConfigLocation上下文参数初始化业务层,持久层的Spring容器,
然后通过DispacherServlet 初始化 展示层的Spring容器 ;
spring父容器 配置时:<context:exclude-filter>的使用原因,为什么在applicationContext.xml中排除controller,而在spring-mvc.xml中include这个controller
既然知道了spring的启动流程,那么web容器初始化webApplicationContext时作为公共的上下文环境,只需要将service、dao等的配置信息在这里加载,而servlet自己的上下文环境信息不需要加载。故,在applicationContext.xml中将@Controller注释的组件排除在外,而在dispatcherServlet加载的配置文件中将@Controller注释的组件加载进来,方便dispatcherServlet进行控制和查找
配置如下:
applicationContext.mxl中:
<context:component-scan base-package='com.linkage.edumanage'>
<context:exclude-filter expression='org.springframework.stereotype.Controller' type='annotation' />
</context:component-scan>
spring-mvc.xml中:
<context:component-scan base-package='com.brolanda.cloud' use-default-filters='false'>
<context:include-filter expression='org.springframework.stereotype.Controller' type='annotation' />
</context:component-scan>
(=======================
问题:
我有个类,web.xml 配置
<servlet>
<servlet-name>vdc_ui</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-servlet.xml</param-value>
<load-on-startup>1</load-on-startup>
</init-param>
</servlet>
applicationContext-servlet.xml中,配置了一个bean,这个bean
<bean id="testA" class="com.bb.testA"></bean>
在controllerer中的某个方法里,使用下面的代码获取:
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
service=(LogInjectionSaf)webApplicationContextgetBean("testA");
为什么显示 service为null呢,怎么才能得到?
答:
获取父容器的 Bean
1.直接注入到Controller里不就好了,何必这么麻烦。
2.若真想如此获取Bean,你可以去实现ApplicationContextAware接口,得到ApplicationContext
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
这个拿到的是父容器,而testA装配在了applicationContext-servlet子容器里。父容器无法取得子容器bean,反之则可以。
因此,要取得子容器里的testA bean,需要先拿到子容器,如下:
Java code
?
1
2
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext,"org.springframework.web.servlet.FrameworkServlet.CONTEXT.vdc_ui" );
TestA service=(TestA)webApplicationContext.getBean("testA");
=======================)
1、<context:annotation-config/>:向Spring容器注册AutowiredAnnotationBeanPostProcessor(@Autowired)、CommonAnnotationBeanPostProcessor(@ Resource 、@ PostConstruct、@ PreDestroy)、PersistenceAnnotationBeanPostProcessor(@PersistenceContext)以及 RequiredAnnotationBeanPostProcessor(@Required) 这 4 个BeanPostProcessor。注意:使用<context:component-scan base-package=”XX.XX”/>后就可以移除<context:annotation-config/>,因为前者包含后者。
2、<mvc:annotation-driven />:向Spring容器注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个映射和适配器bean,@Controller注解使用的前提配置。
3、<context:component-scan>:扫描到有@Component @Controller@Service等这些注解的类就把这些类注册为bean。
4、<mvc:view-controller path="/" view-name="index"/>:当路径为“/”,默认跳转到index页面。
5、<mvc:default-servlet-handler/>:servlet在找页面时,走的是dispatcherServlet路线,找不到的时候会报404加上这个默认的servlet时候,servlet在找不到的时候会去找静态的内容。
6、<mvc:resources mapping="/images/**" location="/images/" cache-period="31556926"/> :匹配URL /images/** 的URL被当做静态资源,由Spring读出到内存中再响应http。
Spring mvc 下 applicationContext 和webApplicationContext
Spring中的ApplicationContexts可以被限制在不同的作用域。在web框架中,每个DispatcherServlet有它自己的WebApplicationContext,它包含了DispatcherServlet配置所需要的bean。DispatcherServlet 使用的缺省BeanFactory是XmlBeanFactory,并且DispatcherServlet在初始化时会在你的web应用的WEB-INF目录下寻找[servlet-name]-servlet.xml文件。DispatcherServlet使用的缺省值可以使用servlet初始化参数修改,
WebApplicationContext仅仅是一个拥有web应用必要功能的普通ApplicationContext。它和一个标准的ApplicationContext的不同之处在于它能够解析主题,并且它知道和那个servlet关联(通过到ServletContext的连接)。WebApplicationContext被绑定在ServletContext上,当你需要的时候,可以使用RequestContextUtils找到WebApplicationContext。
Spring的DispatcherServlet有一组特殊的bean,用来处理请求和显示相应的视图。这些bean包含在Spring的框架里,(可选择)可以在WebApplicationContext中配置,配置方式就象配置其它bean的方式一样。这些bean中的每一个都在下面被详细描述。待一会儿,我们就会提到它们,但这里仅仅是让你知道它们的存在以便我们继续讨论DispatcherServlet。对大多数bean,都提供了缺省值,所有你不必要担心它们的值。
servletContext 是web应用程序的大环境,用于存储整个web应用程序级别的对象,不知道这样说法是否对.
ApplicationContext,WebApplicationContext 是Spring的BeanFactory,从名字中就可以知道区别拉,一个是支持web特性的BeanFactory。
Spring获取WebApplicationContext与ApplicationContext的几种方法:
方法一:在初始化时保存ApplicationContext对象
代码:
ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");
ac.getBean("beanId");
说明:这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况。
方法二:通过Spring提供的工具类获取ApplicationContext对象
代码:
import org.springframework.web.context.support.WebApplicationContextUtils;
ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
ac1.getBean("beanId");
ac2.getBean("beanId");
说明:
这种方式适合于采用Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象,然后在通过它获取需要的类实例。
上面两个工具方式的区别是,前者在获取失败时抛出异常,后者返回null。
其中 servletContext sc 可以具体 换成 servlet.getServletContext()或者 this.getServletContext() 或者 request.getSession().getServletContext(); 另外,由于spring是注入的对象放在ServletContext中的,所以可以直接在ServletContext取出 WebApplicationContext 对象: WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
方法三:继承自抽象类ApplicationObjectSupport
说明:抽象类ApplicationObjectSupport提供getApplicationContext()方法,可以方便的获取到ApplicationContext。
Spring初始化时,会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。
方法四:继承自抽象类WebApplicationObjectSupport
说明:类似上面方法,调用getWebApplicationContext()获取WebApplicationContext
方法五:实现接口ApplicationContextAware
说明:实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 对象。
Spring初始化时,会通过该方法将ApplicationContext对象注入。
在web应用中一般用ContextLoaderListener加载webapplication,如果需要在action之外或者control类之外获取webapplication思路之一是,单独写个类放在static变量中,
然后自启动 DispatcherServlet ,初始化(子Spring容器) WebApplicationContext
并将 业务层, 持久层的Spring 容器作为父容器
好处:
1.
允许 展现 层 和 业务层,持久层更好的解耦 。因为展现层的bean定义在子容器中,而业务层和持久层的bean
定义在 父容器中, 子容器可以访问父容器中的bean,而父容器访问不到
子容器中的bean
2. 可以将刷新操作限定在一定范围内,spring2.1新增1功能,在DispacherServlet上下文的
子Spring容器刷新时,并不影响 其他的容器 ;
3. 允许 分步骤初始化不同层次的Spring容器,通过ContextLoaderListener配合使用
contextConfigLocation上下文参数初始化业务层,持久层的Spring容器,
然后通过DispacherServlet 初始化 展示层的Spring容器 ;
spring父容器 配置时:<context:exclude-filter>的使用原因,为什么在applicationContext.xml中排除controller,而在spring-mvc.xml中include这个controller
既然知道了spring的启动流程,那么web容器初始化webApplicationContext时作为公共的上下文环境,只需要将service、dao等的配置信息在这里加载,而servlet自己的上下文环境信息不需要加载。故,在applicationContext.xml中将@Controller注释的组件排除在外,而在dispatcherServlet加载的配置文件中将@Controller注释的组件加载进来,方便dispatcherServlet进行控制和查找
配置如下:
applicationContext.mxl中:
<context:component-scan base-package='com.linkage.edumanage'>
<context:exclude-filter expression='org.springframework.stereotype.Controller' type='annotation' />
</context:component-scan>
spring-mvc.xml中:
<context:component-scan base-package='com.brolanda.cloud' use-default-filters='false'>
<context:include-filter expression='org.springframework.stereotype.Controller' type='annotation' />
</context:component-scan>
(=======================
问题:
我有个类,web.xml 配置
<servlet>
<servlet-name>vdc_ui</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-servlet.xml</param-value>
<load-on-startup>1</load-on-startup>
</init-param>
</servlet>
applicationContext-servlet.xml中,配置了一个bean,这个bean
<bean id="testA" class="com.bb.testA"></bean>
在controllerer中的某个方法里,使用下面的代码获取:
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
service=(LogInjectionSaf)webApplicationContextgetBean("testA");
为什么显示 service为null呢,怎么才能得到?
答:
获取父容器的 Bean
1.直接注入到Controller里不就好了,何必这么麻烦。
2.若真想如此获取Bean,你可以去实现ApplicationContextAware接口,得到ApplicationContext
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
这个拿到的是父容器,而testA装配在了applicationContext-servlet子容器里。父容器无法取得子容器bean,反之则可以。
因此,要取得子容器里的testA bean,需要先拿到子容器,如下:
Java code
?
1
2
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext,"org.springframework.web.servlet.FrameworkServlet.CONTEXT.vdc_ui" );
TestA service=(TestA)webApplicationContext.getBean("testA");
=======================)
1、<context:annotation-config/>:向Spring容器注册AutowiredAnnotationBeanPostProcessor(@Autowired)、CommonAnnotationBeanPostProcessor(@ Resource 、@ PostConstruct、@ PreDestroy)、PersistenceAnnotationBeanPostProcessor(@PersistenceContext)以及 RequiredAnnotationBeanPostProcessor(@Required) 这 4 个BeanPostProcessor。注意:使用<context:component-scan base-package=”XX.XX”/>后就可以移除<context:annotation-config/>,因为前者包含后者。
2、<mvc:annotation-driven />:向Spring容器注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个映射和适配器bean,@Controller注解使用的前提配置。
3、<context:component-scan>:扫描到有@Component @Controller@Service等这些注解的类就把这些类注册为bean。
4、<mvc:view-controller path="/" view-name="index"/>:当路径为“/”,默认跳转到index页面。
5、<mvc:default-servlet-handler/>:servlet在找页面时,走的是dispatcherServlet路线,找不到的时候会报404加上这个默认的servlet时候,servlet在找不到的时候会去找静态的内容。
6、<mvc:resources mapping="/images/**" location="/images/" cache-period="31556926"/> :匹配URL /images/** 的URL被当做静态资源,由Spring读出到内存中再响应http。
Spring mvc 下 applicationContext 和webApplicationContext
Spring中的ApplicationContexts可以被限制在不同的作用域。在web框架中,每个DispatcherServlet有它自己的WebApplicationContext,它包含了DispatcherServlet配置所需要的bean。DispatcherServlet 使用的缺省BeanFactory是XmlBeanFactory,并且DispatcherServlet在初始化时会在你的web应用的WEB-INF目录下寻找[servlet-name]-servlet.xml文件。DispatcherServlet使用的缺省值可以使用servlet初始化参数修改,
WebApplicationContext仅仅是一个拥有web应用必要功能的普通ApplicationContext。它和一个标准的ApplicationContext的不同之处在于它能够解析主题,并且它知道和那个servlet关联(通过到ServletContext的连接)。WebApplicationContext被绑定在ServletContext上,当你需要的时候,可以使用RequestContextUtils找到WebApplicationContext。
Spring的DispatcherServlet有一组特殊的bean,用来处理请求和显示相应的视图。这些bean包含在Spring的框架里,(可选择)可以在WebApplicationContext中配置,配置方式就象配置其它bean的方式一样。这些bean中的每一个都在下面被详细描述。待一会儿,我们就会提到它们,但这里仅仅是让你知道它们的存在以便我们继续讨论DispatcherServlet。对大多数bean,都提供了缺省值,所有你不必要担心它们的值。
servletContext 是web应用程序的大环境,用于存储整个web应用程序级别的对象,不知道这样说法是否对.
ApplicationContext,WebApplicationContext 是Spring的BeanFactory,从名字中就可以知道区别拉,一个是支持web特性的BeanFactory。
Spring获取WebApplicationContext与ApplicationContext的几种方法:
方法一:在初始化时保存ApplicationContext对象
代码:
ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");
ac.getBean("beanId");
说明:这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况。
方法二:通过Spring提供的工具类获取ApplicationContext对象
代码:
import org.springframework.web.context.support.WebApplicationContextUtils;
ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);
ac1.getBean("beanId");
ac2.getBean("beanId");
说明:
这种方式适合于采用Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象,然后在通过它获取需要的类实例。
上面两个工具方式的区别是,前者在获取失败时抛出异常,后者返回null。
其中 servletContext sc 可以具体 换成 servlet.getServletContext()或者 this.getServletContext() 或者 request.getSession().getServletContext(); 另外,由于spring是注入的对象放在ServletContext中的,所以可以直接在ServletContext取出 WebApplicationContext 对象: WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
方法三:继承自抽象类ApplicationObjectSupport
说明:抽象类ApplicationObjectSupport提供getApplicationContext()方法,可以方便的获取到ApplicationContext。
Spring初始化时,会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。
方法四:继承自抽象类WebApplicationObjectSupport
说明:类似上面方法,调用getWebApplicationContext()获取WebApplicationContext
方法五:实现接口ApplicationContextAware
说明:实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 对象。
Spring初始化时,会通过该方法将ApplicationContext对象注入。
在web应用中一般用ContextLoaderListener加载webapplication,如果需要在action之外或者control类之外获取webapplication思路之一是,单独写个类放在static变量中,