spring 组件扫描

我在堆栈溢出中遇到了一个有趣的问题。 Brett Ryan有问题,Spring Security配置被初始化了两次。 当我查看他的代码时,我发现了问题所在。 让我展示显示代码。

他有相当标准的Spring应用程序(不使用Spring Boot)。 使用基于Spring的AbstractAnnotationConfigDispatcherServletInitializer更现代的Java servlet配置。

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;public class AppInitializer extendsAbstractAnnotationConfigDispatcherServletInitializer {@Overrideprotected Class<?>[] getRootConfigClasses() {return new Class[]{SecurityConfig.class};}@Overrideprotected Class<?>[] getServletConfigClasses() {return new Class[]{WebConfig.class};}@Overrideprotected String[] getServletMappings() {return new String[]{"/"};}}

如您所见,有两个配置类:

  • SecurityConfig –保存Spring Security配置
  • WebConfig – Spring的主要IoC容器配置
package net.lkrnac.blog.dontscanconfigurations;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Autowiredpublic void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {System.out.println("Spring Security init...");auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");}}
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "net.lkrnac.blog.dontscanconfigurations")
public class WebConfig extends WebMvcConfigurerAdapter {}

注意WebConfig的组件扫描。 这是扫描软件包,所有三个类都位于该软件包中。 在servlet容器上运行此命令时,将文本“ Spring Security init…”写入控制台两次。 这意味着SecurityConfig配置被加载两次。 它已加载:

  1. 在方法AppInitializer.getRootConfigClasses()的servlet容器初始化期间
  2. 通过类WebConfig组件扫描

为什么? 我在Spring的文档中找到了这种解释 :

请记住, @Configuration类使用@Component进行元注释 ,因此它们是组件扫描的候选对象!

因此,这是Spring的功能,因此我们希望避免Servlet配置使用的Spring @Configuration组件扫描。 Brett Ryan独立发现了这个问题,并在提到的Stack Overflow问题中展示了他的解决方案:

@ComponentScan(basePackages = "com.acme.app",excludeFilters = {@Filter(type = ASSIGNABLE_TYPE,value = {WebConfig.class,SecurityConfig.class})})

我不喜欢这种解决方案。 注释对我来说太冗长了。 另外,一些开发人员可以创建新的@Configuration类,而忘记将其包含在此过滤器中。 我宁愿指定将被Spring的组件扫描排除的特殊软件包。

  • 我在Github上创建了示例项目,以便您可以使用它。

翻译自: https://www.javacodegeeks.com/2014/12/avoid-unwanted-component-scanning-of-spring-configuration.html

spring 组件扫描

spring 组件扫描_避免不必要的Spring配置组件扫描相关推荐

  1. java spring框架 注解_详解Java的Spring框架中的注解的用法

    1. 使用Spring注解来注入属性 1.1. 使用注解以前我们是怎样注入属性的类的实现: class UserManagerImpl implements UserManager { private ...

  2. 引入antd组件样式_如何使用 dumi 和 fatherbuild 创建组件库

    在这个文章中,将会简单的介绍如何使用 dumi 和 father-build 来创建组件库和维护组件库.最终我们会演示如何组织组件库的代码.还会输出一个简单的组件库. 脚手架初始化 新建一个空文件夹, ...

  3. redis spring 切面缓存_今日份学习: Spring中使用AOP并实现redis缓存?

    笔记 在Spring中如何使用AOP? Spring是如何切换JDK动态代理和CGLIB的? spring.aop.proxy-target-class=true (在下方第二个链接中,原生doc中提 ...

  4. spring 定时器注释_带注释的控制器– Spring Web / Webflux和测试

    spring 定时器注释 Spring Webflux和Spring Web是两个完全不同的Web堆栈. 但是, Spring Webflux继续支持基于注释的编程模型 使用这两个堆栈定义的端点可能看 ...

  5. spring 动态代理_分析动态代理给 Spring 事务埋下的坑

    前言 Spring的声明式事务让我们不在编写获得连接.关闭连接.开启事务.提交事务.回滚事务等代码,通过一个简单的@Transactional注解,就让我们轻松进行事务处理.我们知道Spring事务基 ...

  6. react第三方组件库_如何自定义您的第三方React组件

    react第三方组件库 by Jacob Goh 雅各布·高 如何自定义您的第三方React组件 (How to customize your third party React components ...

  7. 分布式 集群 系统组件架构_分布式跟踪系统的四个组件如何一起工作

    分布式 集群 系统组件架构 十年前,基本上只有认真思考分布式跟踪的人是学者和少数大型互联网公司. 如今,对于任何采用微服务的组织来说,它已经变成了赌注. 基本原理是公认的:微服务以令人惊讶且通常是惊人 ...

  8. 20.Spring学习笔记_基于配置文件的方式来配置 AOP(by尚硅谷_佟刚)

    基于 XML 的配置声明切面 除了使用 AspectJ 注解声明切面, Spring 也支持在 Bean 配置文件中声明切面. 这种声明是通过 aop schema 中的 XML 元素完成的. 正常情 ...

  9. vue 动态设置组件高度_高度动态的Vue明星评分组件

    vue 动态设置组件高度 虚拟动态星级 (vue-dynamic-star-rating) A Highly Customizable, easy-to-use elegant stars ratin ...

最新文章

  1. 《阿里巴巴编码规范(JAVA)》学习认证考后感
  2. linux修改ssh端口
  3. 用原生JavaScript实现图片瀑布流的浏览效果
  4. 基于MIG控制器的DDR3读写控制详解
  5. ercp手术为什么那么贵_尼泊尔佛像为什么那么贵?
  6. ArcGIS 10.6连接Access 2007(.accdb)及以上版本数据库
  7. cesium 经纬度绘制点_炫酷大屏地图自定义绘制(一)
  8. 【Python】理解Python(2) - help() 函数? or 类?
  9. l开头的英文车标是什么车_行业冷知识 | 为什么汽车品牌都喜欢用动物做车标?...
  10. 2013.07.10《播音主持之绕口令训练…
  11. 扬州大学2022年858程序设计与数据结构试题参考答案
  12. 主板怎么开启csm_手把手教你查看电脑主板是否支持UEFI+GPT启动模式-网络教程与技术 -亦是美网络...
  13. Express访问静态资源(express.static)
  14. 委托 和 事件 总括:
  15. pdf转图片可调整大小分辨率
  16. vmvare虚拟机无法读取ntfs的U盘解决方法,以及更换镜像下载源
  17. html语言制作表格模板,十个最简单实用的Table设计模板
  18. [Vue warn]: Unknown custom element: <helptext> - did you register the component correctly? For recu
  19. Redis缓存的雪崩、穿透、击穿
  20. 期货板幅什么意思(期货涨停板什么意思)

热门文章

  1. P3226-[HNOI2012]集合选数【状压dp】
  2. nssl1175-小S练跑步【bfs】
  3. 2021牛客暑期多校训练营7 F-xay loves trees(线段树+滑动窗口)
  4. 2021牛客暑期多校训练营7 J-xay loves Floyd(最短路+bitset优化集合交)
  5. 【最短路】【Floyed】医院设置(ssl 1614)
  6. 有上下界网络流问题汇总
  7. MySQL str_to_date()函数
  8. HashMap jdk1.7源码阅读与解析
  9. Jdk8一行代码读取文件
  10. C++描述杭电OJ 2016. 数据的交换输出 ||