欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

解決Spring Boot 多模塊注入訪問不到j(luò)ar包中的Bean問題

 更新時間:2020年09月26日 10:56:00   作者:JKong的二次學(xué)習(xí)日記  
這篇文章主要介紹了解決Spring Boot 多模塊注入訪問不到j(luò)ar包中的Bean問題。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

情景描述

一個聚合項目spring-security-tutorial,其中包括4個module,pom如下所示:

<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.github.jdkong.security</groupId>
  <artifactId>spring-security-tutorial</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <modules>``
    <module>security-core</module>
    <module>security-app</module>
    <module>security-browser</module>
    <module>security-demo</module>
  </modules>
 
 <!-- 其他部分省略--> 
</project>

在此項目中,子項目security-browser是一個簡單的maven項目,打成jar包,供security-demo使用,security-demo項目是一個springboot項目。

問題描述

在security-browser項目中自動注入了一個配置類,如下所示:

/**
 * @author jdkong
 */
@Slf4j
@Configuration
public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter {
  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.formLogin()
        .and()
        .authorizeRequests()
        .anyRequest()
        .authenticated();
  }
}

在security-demo中使用此配置類時,不起作用。

問題分析

導(dǎo)致此類問題的主要原因是,此類不在Spring Boot的組件掃描范圍之內(nèi)。

1. 關(guān)于 Spring Boot 自動注入及組件掃描

在平時使用 Spring Boot 時,常常會使用到@Configuration,@Contoller,@Service,@Component等注解,被添加這些注解的類,在 Spring Boot 啟動時,會自動被 Spring 容器管理起來。

上面提到了,添加了一些注解的類會在Spring Boot 容器啟動時,被加載到Spring 容器中。那么,組件掃描的作用就是:當(dāng) Spring Boot 啟動時,根據(jù)定義的掃描路徑,把符合掃描規(guī)則的類裝配到spring容器中。

2. Spring Boot 中 @ComponentScan

簡單的介紹了@ComponentScan的基礎(chǔ)作用,這個注解為我們使用提供了一些可自定義配置屬性,先來看看@ComponentScan注解源碼:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Repeatable(ComponentScans.class)
public @interface ComponentScan {
  // 指定掃描包的位置(同:basePackages 屬性),可以是單個路徑,也可以是掃描的路徑數(shù)組
  @AliasFor("basePackages")
  String[] value() default {};
 // 指定掃描包的位置(同:value 屬性)
  @AliasFor("value")
  String[] basePackages() default {};
 // 指定具體的掃描的類
  Class<?>[] basePackageClasses() default {};
 // bean的名稱的生成器 
  Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;

  Class<? extends ScopeMetadataResolver> scopeResolver() default AnnotationScopeMetadataResolver.class;

  ScopedProxyMode scopedProxy() default ScopedProxyMode.DEFAULT;
 // 控制符合組件檢測條件的類文件  默認(rèn)是包掃描下的 **/*.class
  String resourcePattern() default "**/*.class";
  // 是否開啟對@Component,@Repository,@Service,@Controller的類進行檢測
  boolean useDefaultFilters() default true;
  // 包含的過濾條件
  // 1. FilterType.ANNOTATION:  按照注解過濾
  // 2. FilterType.ASSIGNABLE_TYPE:   按照給定的類型
  // 3. FilterType.ASPECTJ:  使用ASPECTJ表達(dá)式
  // 4. FilterType.REGEX:   正則
  // 5. FilterType.CUSTOM:  自定義規(guī)則
  ComponentScan.Filter[] includeFilters() default {};
  // 排除的過濾條件,用法和includeFilters一樣
  ComponentScan.Filter[] excludeFilters() default {};

  boolean lazyInit() default false;

  @Retention(RetentionPolicy.RUNTIME)
  @Target({})
  public @interface Filter {
    FilterType type() default FilterType.ANNOTATION;
    @AliasFor("classes")
    Class<?>[] value() default {};
    @AliasFor("value")
    Class<?>[] classes() default {};
    String[] pattern() default {};
  }
}

總結(jié)一下@ComponentScan的常用方式如下:

通過使用value,basePackages屬性來指定掃描范圍;

自定掃描路徑下邊帶有@Controller,@Service,@Repository,@Component注解加入Spring容器

通過includeFilters加入掃描路徑下沒有以上注解的類加入spring容器

通過excludeFilters過濾出不用加入spring容器的類

自定義增加了@Component注解的注解方式

3. Spring Boot 中 @SpringBootApplication

在創(chuàng)建Spring Boot 項目之后,在默認(rèn)的啟動類上會被添加@SpringBootApplication注解,這個注解默認(rèn)幫我們開啟一些自動配置的功能,比如:基于Java的Spring配置,組件掃描,特別是用于啟用Spring Boot的自動配置功能。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration          // 允許自動配置
@ComponentScan(
  excludeFilters = {@Filter(       // 定義排除規(guī)則
  type = FilterType.CUSTOM,        // 采用自定義的方式
  classes = {TypeExcludeFilter.class}   // 自定義實現(xiàn)邏輯
), @Filter(                 // 同上
  type = FilterType.CUSTOM,
  classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
  
  // 為 @EnableAutoConfiguration 添加 exclude 規(guī)則
  @AliasFor(
    annotation = EnableAutoConfiguration.class,
    attribute = "exclude"
  )
  Class<?>[] exclude() default {};
  
  // 為 @EnableAutoConfiguration 添加 excludeName 規(guī)則
  @AliasFor(
    annotation = EnableAutoConfiguration.class,
    attribute = "excludeName"
  )
  String[] excludeName() default {};
  
  // 為 @ComponentScan 添加 basePackages 規(guī)則
  @AliasFor(
    annotation = ComponentScan.class,
    attribute = "basePackages"
  )
  String[] scanBasePackages() default {};

  // 為 @ComponentScan 添加 basePackageClasses 規(guī)則
  @AliasFor(
    annotation = ComponentScan.class,
    attribute = "basePackageClasses"
  )
  Class<?>[] scanBasePackageClasses() default {};
}

從上面的源碼部分可以看到,@SpringBootApplication是一個組合注解,也就相當(dāng)于使用一個@SpringBootApplication可以替代@SpringBootConfiguration,@EnableAutoConfiguration,@ComponentScan幾個注解聯(lián)合使用。

注:此注釋從SpringBoot 1.2開始提供,這意味著如果你運行的是較低的版本,并且如果你需要這些功能,你需要手動添加@Configuration,@CompnentScan和@EnableAutoConfiguration。

那么,可能會有這樣的問題,我只是使用了一個@SpringBootApplication注解,但是我如何對@ComponentScan的屬性做自定義配置呢?

當(dāng)然,Spring 團隊已經(jīng)很好的解決了這個問題,在@SpringBootApplication注解類中的屬性上添加@AliasFor注解,從而實現(xiàn)通過對@SpringBootApplication中的屬性進行自定義,達(dá)到對對應(yīng)的注解的屬性的自定義。

比如:

@AliasFor(
  annotation = ComponentScan.class,
  attribute = "basePackages"
)
String[] scanBasePackages() default {};

這段代碼就是實現(xiàn),通過對@SpringBootApplication的屬性scanBasePackages,實現(xiàn)對@ComponentScan中的屬性basePackages進行自定義。

4. 回答開篇問題

先看看項目結(jié)構(gòu),項目入口文件在子項目security-demo中,并且入口類所在包位置為:package com.github.jdkong.security。

也就是說,在不做任何配置的情況下,此項目只會掃描當(dāng)前包路徑及其子路徑下的文件,并將符合條件的對象注入到容器中管理。

再看看配置文件所在的包路徑位置:package com.github.jdkong.browser.config,可見此包路徑并不在項目掃描的路徑范圍之內(nèi)。

這也就導(dǎo)致了,我們定義的配置類,雖然加了@Configuration也不會對我們的項目起到作用。

可以對項目注解進行稍微修改,制定掃描包的范圍,就可以簡單的解決這個問題。如下:

@SpringBootApplication(scanBasePackages="com.github.jdkong")
public class SecurityApplication {

  public static void main(String[] args) {
    SpringApplication.run(SecurityApplication.class,args);
  }
}

5. 補充說明:@AliasFor

在Spring注解中,經(jīng)常會發(fā)現(xiàn)很多注解的不同屬性起著相同的作用,比如@ComponentScan的value屬性和basePackages屬性。所以在使用的時候就需要做一些基本的限制,比如value和basePackages的值不能沖突,比如任意設(shè)置value或者設(shè)置basePackages屬性的值,都能夠通過另一個屬性來獲取值等等。為了統(tǒng)一處理這些情況,Spring創(chuàng)建了@AliasFor標(biāo)簽。

以上這篇解決Spring Boot 多模塊注入訪問不到j(luò)ar包中的Bean問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot構(gòu)建Restful service完成Get和Post請求

    SpringBoot構(gòu)建Restful service完成Get和Post請求

    這篇文章主要介紹了SpringBoot構(gòu)建Restful service完成Get和Post請求的示例代碼,感興趣的朋友一起看看吧
    2017-08-08
  • java實現(xiàn)HttpClient異步請求資源的方法

    java實現(xiàn)HttpClient異步請求資源的方法

    這篇文章主要介紹了java實現(xiàn)HttpClient異步請求資源的方法,實例分析了java基于http協(xié)議實現(xiàn)異步請求的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • SpringBoot項目加入沖突動態(tài)監(jiān)測算法的實現(xiàn)

    SpringBoot項目加入沖突動態(tài)監(jiān)測算法的實現(xiàn)

    沖突動態(tài)監(jiān)測算法是一種網(wǎng)絡(luò)通信中的沖突檢測方法,適用于無線網(wǎng)絡(luò)或其他共享傳輸介質(zhì)的環(huán)境,本文主要介紹了SpringBoot項目加入沖突動態(tài)監(jiān)測算法的實現(xiàn),感興趣的可以了解一下
    2023-09-09
  • Java實現(xiàn)對象轉(zhuǎn)CSV格式

    Java實現(xiàn)對象轉(zhuǎn)CSV格式

    CSV是一種逗號分隔值格式的文件,一般用來存儲數(shù)據(jù)的純文本格式文件。Java對象轉(zhuǎn)CSV,有現(xiàn)成的工具包,commons-lang3 的ReflectionToStringBuilder 就可以簡單的解決的對象轉(zhuǎn)CSV,快跟隨小編一起學(xué)習(xí)一下吧
    2022-06-06
  • SpringCloud hystrix服務(wù)降級概念介紹

    SpringCloud hystrix服務(wù)降級概念介紹

    什么是服務(wù)降級?當(dāng)服務(wù)器壓力劇增的情況下,根據(jù)實際業(yè)務(wù)情況及流量,對一些服務(wù)和頁面有策略的不處理或換種簡單的方式處理,從而釋放服務(wù)器資源以保證核心交易正常運作或高效運作
    2022-09-09
  • 基于swing開發(fā)彈幕播放器

    基于swing開發(fā)彈幕播放器

    這篇文章主要為大家詳細(xì)介紹了基于swing實現(xiàn)彈幕播放器的開發(fā)過程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Spring MVC 文件上傳下載的實例

    Spring MVC 文件上傳下載的實例

    本篇文章主要介紹了Spring MVC 文件上傳下載的實例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • Spring Boot 集成 Kafka的詳細(xì)步驟

    Spring Boot 集成 Kafka的詳細(xì)步驟

    Spring Boot與Kafka的集成使得消息隊列的使用變得更加簡單和高效,可以配置 Kafka、實現(xiàn)生產(chǎn)者和消費者,并利用 Spring Boot 提供的功能處理消息流,以下是 Spring Boot 集成 Kafka 的詳細(xì)步驟,包括配置、生產(chǎn)者和消費者的實現(xiàn)以及一些高級特性,感興趣的朋友一起看看吧
    2024-07-07
  • spring security在分布式項目下的配置方法(案例詳解)

    spring security在分布式項目下的配置方法(案例詳解)

    這篇文章主要介紹了spring security在分布式項目下的配置方法,本文通過一個項目案例給大家詳細(xì)介紹,通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • java中基本注解的知識點總結(jié)

    java中基本注解的知識點總結(jié)

    在本篇文章里小編給大家整理的是一篇關(guān)于java中基本注解的知識點總結(jié),有需要的朋友們可以跟著學(xué)習(xí)下。
    2021-06-06

最新評論