日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

spring 组件扫描_避免不必要的Spring配置组件扫描

發(fā)布時(shí)間:2023/12/3 javascript 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring 组件扫描_避免不必要的Spring配置组件扫描 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

spring 組件掃描

我在堆棧溢出中遇到了一個(gè)有趣的問題。 Brett Ryan有問題,Spring Security配置被初始化了兩次。 當(dāng)我查看他的代碼時(shí),我發(fā)現(xiàn)了問題所在。 讓我展示顯示代碼。

他有相當(dāng)標(biāo)準(zhǔn)的Spring應(yīng)用程序(不使用Spring Boot)。 使用基于Spring的AbstractAnnotationConfigDispatcherServletInitializer更現(xiàn)代的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[]{"/"};}}

如您所見,有兩個(gè)配置類:

  • 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的組件掃描。 這是掃描軟件包,所有三個(gè)類都位于該軟件包中。 在servlet容器上運(yùn)行此命令時(shí),將文本“ Spring Security init…”寫入控制臺(tái)兩次。 這意味著SecurityConfig配置被加載兩次。 它已加載:

  • 在方法AppInitializer.getRootConfigClasses()的servlet容器初始化期間
  • 通過類WebConfig組件掃描
  • 為什么? 我在Spring的文檔中找到了這種解釋 :

    請記住, @Configuration類使用@Component進(jìn)行元注釋 ,因此它們是組件掃描的候選對象!

    因此,這是Spring的功能,因此我們希望避免Servlet配置使用的Spring @Configuration組件掃描。 Brett Ryan獨(dú)立發(fā)現(xiàn)了這個(gè)問題,并在提到的Stack Overflow問題中展示了他的解決方案:

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

    我不喜歡這種解決方案。 注釋對我來說太冗長了。 另外,一些開發(fā)人員可以創(chuàng)建新的@Configuration類,而忘記將其包含在此過濾器中。 我寧愿指定將被Spring的組件掃描排除的特殊軟件包。

    • 我在Github上創(chuàng)建了示例項(xiàng)目,以便您可以使用它。

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

    spring 組件掃描

    總結(jié)

    以上是生活随笔為你收集整理的spring 组件扫描_避免不必要的Spring配置组件扫描的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。