广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Spring和SpringMVC扫描注解类冲突的解决方案
  • 637
分享到

Spring和SpringMVC扫描注解类冲突的解决方案

2024-04-02 19:04:59 637人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录spring和springMVC扫描注解类冲突最正确的配置方式也可以用直接扫描的方式几种不同配置的测试Spring和Springmvc注解扫描注意事项现象方法Spring和Spr

Spring和SpringMVC扫描注解类冲突

最正确的配置方式

在主容器中applicationContext.xml中,将Controller的注解排除掉


 <context:component-scan base-package="com"> 
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> 
  </context:component-scan> 

而在springmvc.xml中,将Service注解给去掉


  <context:component-scan base-package="com"> 
  <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> 
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> 
  </context:component-scan> 

因为spring的context是父子容器,所以会产生冲突,由ServletContextListener产生的是父容器,springMVC产生的是子容器,子容器Controller进行扫描装配时装配了@Service注解的实例,而该实例理应由父容器进行初始化以保证事务的增强处理,所以此时得到的将是原样的Service(没有经过事务加强处理,故而没有事务处理能力。

还有一种方式是将service层改用xml配置,其实这样做也是变相的让springmvc无法扫描service,而只能依赖父窗口也就是ServletContextListener来进行初始化,这样同样被赋予了事务性。

也可以用直接扫描的方式

直接扫描比较省事,但是事务回得不到处理,所以在具体的层面上还需要加入注解去声明事务,比如在dao层和service层加入@Transactional

几种不同配置的测试

(1)只在applicationContext.xml中配置如下


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

启动正常,但是任何请求都不会被拦截,简而言之就是@Controller失效,出现404错误

(2)只在springmvc.xml中配置


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

启动正常,请求也正常,但是事物失效,也就是不能进行回滚

(3)在applicationContext.xml和springmvc.xml中都配置


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

启动正常,请求正常,也是事物失效,不能进行回滚

(4)在applicationContext.xml中配置如下


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

在springmvc.xml中配置如下


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

或者按最正确的配置applicationContext.xml,将Controller的注解排除掉 ,springmvc.xml,将Service注解给去掉

此时启动正常,请求正常,事物也正常了。

Spring和SpringMVC注解扫描注意事项

现象

Springmvc和Spring设置自动扫描文件夹自动注入bean的时候有时候出现冲突

方法

1:springmvc设置只扫描controller

这里写图片描述

2:spring设置不扫描controller

这里写图片描述

代码:


springmvc 的扫描设置只扫描controller  spring的扫描设置为不扫描controller的 防止重复注入bean管理   (出现未错误)
<!-- 设置使用Spring注解的类所在的jar包  设置不扫描下面规则的 -->
    <context:component-scan base-package="com.oig">
        <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注解的类所在的jar包 设置只扫描自定义规则的-->
    <context:component-scan base-package="com.oig" use-default-filters="false">
        <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>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: Spring和SpringMVC扫描注解类冲突的解决方案

本文链接: https://www.lsjlt.com/news/137599.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作