关于springBoot版本升级后拦截器注入的问题
今天发现一个很奇怪的问题 我的springBoot版本是2.0.3,其中拦截器和配置类如下所示,可以完美运行 拦截器
配置类
当我把springBoot版本升级到2.3.3,奇怪的事情发生了,拦截器注入失败,项目无法启动
2022-10-12 17:13:03.735 WARN 12008 --- [on(3)-127.0.0.1] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'loginCheckHandlerInterceptor' defined in class path resource [com/xxx/config/SessionConfigurer.class]: Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=sessionConfigurer; factoryMethodName=loginCheckHandlerInterceptor; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/xxx/config/SessionConfigurer.class]] for bean 'loginCheckHandlerInterceptor': There is already [Generic bean: class [com.zwaytech.iomp.commom.interceptor.LoginCheckHandlerInterceptor]; scope=singleton; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [D:\soft\apache-tomcat-8.5.56 - 鍓湰\webapps\operations\WEB-INF\classes\com\xxx\commom\interceptor\LoginCheckHandlerInterceptor.class]] bound.
尝试过各种解决方法,当我无意间把LoginCheckHandlerInterceptor.java类上的@Component注解去掉之后,项目又神奇的恢复了。
我想应该是LoginCheckHandlerInterceptor.java类中的@Component注解和配置类中的@Bean注解冲突了导致项目启动失败,两个注解都有将一个类标注为spring bean的作用,但是在升级前为什么没有问题呢。有没有大佬知道原理的。