zoukankan      html  css  js  c++  java
  • Spring的父子容器问题

    在ssm框架搭建的时候

    配置了一个Spring容器,又配置了一个前端控制器

    <!-- 初始化spring容器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext-*.xml</param-value>
    </context-param>
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--springmvc前端控制器-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, 
            springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
        <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>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    子容器可以访问父容器的对象,父容器不能访问子容器中的对象

    对于属性又不一样了,如果在父容器中有properties文件,其中的属性子容器是访问不到的!跨容器了!!

    <属性互相都访问不到>

    所以在Controller中可以注入一个Service对象

    所有的Controller对象都放在springmvc容器中

    springmvc.xml

    ......
    <!--配置包扫描器-->
    <context:component-scan base-package="com.winner.controller"/>
    ......

    只扫controller,不要扫多了,如果springmvc容器扫多了,那么父子容器都有一套service,controller肯定用自容器的service,在一块嘛!那么在父容器中配置的事务就不生效了!!!

    Service和Mapper对象都放在Spring容器中

    applicationContext-dao.xml

    .......
    <!--包扫描器,扫描代理mapper-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.winner.mapper"/>
    </bean>
    ......

    applicationContext-service.xml

    ......
    <!--包扫描器,扫描带@Service注解的类-->
    <context:component-scan base-package="com.winner.service"/>
    ......

     

  • 相关阅读:
    函数,封装
    c++增强c
    Git安装与使用
    Ajax&Jquery
    IntelliJ Idea2018.2,Pycharm20183.2破解教程
    事务&数据库连接池&DBUtils&JSP设计模式
    JSP&EL&JSTL
    Http协议和Servlet
    接收流信息---字符串
    lucene索引库优化一
  • 原文地址:https://www.cnblogs.com/winner-0715/p/5511712.html
Copyright © 2011-2022 走看看