spring中父子容器
在spring容器中,分有父容器和子容器
父容器是使用了ContextLoaderListener加载并实例化的ioc容器为父容器
子容器是使用了DispatcherServerlet加载并实例化的ioc容器为子容器
他们之间的关系是
父容器不能调用子容器中任何的组件,子容器可以调用除了controller以外的组件.
列如父容器要调用子容器中dubbo的服务,这时是无法调用的,会出现空指针异常,只能通过父容器再实例化一次dubbo才能调用.
DispatcherServlet内实例化dubbo容器为子容器,如下
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定加载的配置文件 ,通过参数 contextConfigLocation 加载 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
ContextLoaderListener内实例化security容器为父容器,如下
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-*.xml</param-value>
</context-param>
下面是springmvc.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean
class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<context:component-scan base-package="cn.ryq.shop.controller"/>
<!-- 引用 dubbo 服务 -->
<dubbo:application name="pinyougou-shop-web">
<!-- 不开启 qos ; qos 可通过指令动态调动提供的服务 -->
<dubbo:parameter key="qos.enable" value="false"/>
</dubbo:application>
<dubbo:registry address="zookeeper://192.168.12.168:2181"/>
<dubbo:annotation package="cn.ryq.shop.controller" />
</beans>
下面是security.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 引用 dubbo 服务 -->
<dubbo:application name="pinyougou-shop-web" >
<!-- 不开启 qos ; qos 可通过指令动态调动提供的服务 -->
<dubbo:parameter key="qos.enable" value="false"/>
</dubbo:application>
<dubbo:registry address="zookeeper://192.168.12.168:2181"/>
<dubbo:annotation package="cn.ryq.shop.service.impl" />
<http pattern="/shoplogin.html" security="none"/>
<http pattern="/register.html" security="none"/>
<http pattern="/seller/add.do" security="none"/>
<http pattern="/js/**" security="none"/>
<http pattern="/css/**" security="none"/>
<http pattern="/img/**" security="none"/>
<http pattern="/plugins/**" security="none"/>
<http use-expressions="false">
<intercept-url pattern="/**" access="ROLE_SELLER"/>
<form-login login-page="/shoplogin.html" authentication-success-forward-url="/admin/index.html"
authentication-failure-forward-url="/shoplogin.html" always-use-default-target="true"/>
<csrf disabled="true"/>
<headers>
<frame-options policy="SAMEORIGIN"/>
</headers>
<logout/>
</http>
<beans:bean class="org.springframework.security.crypto.password.NoOpPasswordEncoder" id="passwordEncoder"/>
<beans:bean class="cn.ryq.shop.service.impl.UserDetailsServiceImpl" id="userDetailsService"/>
<authentication-manager>
<authentication-provider user-service-ref="userDetailsService">
<password-encoder ref="passwordEncoder"/>
</authentication-provider>
</authentication-manager>
</beans:beans>
下面是父容器要调用子容器中的组件,dubbo的服务对象
public class UserDetailsServiceImpl implements UserDetailsService {
@Reference(timeout = 3000)
private SellerService sellerService;
posted on 2019-08-19 17:10 JamesHarden1121 阅读(675) 评论(0) 编辑 收藏 举报