zoukankan      html  css  js  c++  java
  • springmvc 整合Controller出现实例化两次问题

    启动项目的时候,发现初始化控制层的时候,初始化(使用构造方法打印日志的方式)了两次的情况。

    后来检查配置:

    <context:component-scan base-package="com.winning.joiner">
            
    </context:component-scan>

    发现,配置扫描的时候,没有排除@controller 层的过滤器

    修改后,问题解决,如下:

    在springmvc的配置文件配置扫描:

      <context:component-scan base-package="com.winning.joiner.magiccube">
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
            <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
        </context:component-scan>

    在spring context的配置扫描:

      <context:component-scan base-package="com.winning.joiner.magiccube">
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
            <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
        </context:component-scan>

    两个文件配合配置,才能解决实例化两次的问题。

    简单点说,就是springmvc扫描的包,扫描包含@Controller注解的类。

    而spring context 的扫描包,就要包包含@Controller的注解的类排除掉,这样才会在spring整个大工厂中,存在一个实例。

  • 相关阅读:
    Python sendemail txt,html,图片及附件
    python 3 requests库2个问题
    py37 unitest+html_sendmail
    python 3 unitest批量执行用例
    phthon 3 unittest模块使用
    python 之发送邮件
    ipad已停用 连接itunes怎么办
    Ubuntu 16.04系统挂载4T硬盘
    华硕RT-AC86U路由器 AP模式实现多路由器组网,扩展主路由器的无线网范围
    ubuntu 常用命令
  • 原文地址:https://www.cnblogs.com/sloveling/p/spirngmvc_instance.html
Copyright © 2011-2022 走看看