使用@ComponentScan自動掃描組件
案例准備
1.創建一個配置類,在配置類上添加 @ComponentScan 注解。該注解默認會掃描該類所在的包下所有的配置類,相當於之前的 <context:component-scan>。
package io.mieux.config;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan
public class BeanConfig {
}
2.使用 ApplicationContext 的 getBeanDefinitionNames() 方法獲取已經注冊到容器中的 bean 的名稱。
import io.mieux.config.BeanConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class App02 {
public static void main(String[] args) {
ApplicationContext applicationContext =
new AnnotationConfigApplicationContext(BeanConfig.class);
String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
for (String beanName : beanDefinitionNames) {
System.out.println("beanName: " + beanName);
}
}
}
運行效果:
beanName: org.springframework.context.annotation.internalConfigurationAnnotationProcessor
beanName: org.springframework.context.annotation.internalAutowiredAnnotationProcessor
beanName: org.springframework.context.annotation.internalRequiredAnnotationProcessor
beanName: org.springframework.context.annotation.internalCommonAnnotationProcessor
beanName: org.springframework.context.event.internalEventListenerProcessor
beanName: org.springframework.context.event.internalEventListenerFactory
beanName: beanConfig
除了 spring 本身注冊的一些 bean 之外,可以看到最后一行,已經將BeanConfig
這個類注冊進容器中了。
使用@ComponentScan 的 valule屬性配置
3.指定要掃描的包(使用@ComponentScan 的 valule
屬性來配置)
創建一個controller 包,並在該包下新建一個 AppController 類。
package io.mieux.controller;
import org.springframework.stereotype.Controller;
@Controller
public class AppController {
}
在類上加了@Controller注解,說明該類是一個 Component。在 BeanConfig 類中修改:
package io.mieux.config;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan(value = "io.mieux.controller")
public class BeanConfig {
}
在 @ComponentScan
注解中指定了要掃描的包。
運行效果:
beanName: org.springframework.context.annotation.internalConfigurationAnnotationProcessor
beanName: org.springframework.context.annotation.internalAutowiredAnnotationProcessor
beanName: org.springframework.context.annotation.internalRequiredAnnotationProcessor
beanName: org.springframework.context.annotation.internalCommonAnnotationProcessor
beanName: org.springframework.context.event.internalEventListenerProcessor
beanName: org.springframework.context.event.internalEventListenerFactory
beanName: beanConfig
beanName: appController
AppController 已經被注冊進容器了。
excludeFilters 和 includeFilters 的使用
使用 excludeFilters 來按照規則排除某些包的掃描。
package io.mieux.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
@ComponentScan(value = "io.mieux",
excludeFilters = {@Filter(type = FilterType.ANNOTATION,
value = {Controller.class})})
public class BeanConfig {
}
excludeFilters 的參數是一個 Filter[] 數組,然后指定 FilterType 的類型為 ANNOTATION,也就是通過注解來過濾,最后的 value 則是Controller 注解類。配置之后,在 spring 掃描的時候,就會跳過 io.mieux 包下,所有被 @Controller 注解標注的類。
使用 includeFilters 來按照規則只包含某些包的掃描。
在創建一個 service 的包,並創建一個 AppService 類,再加上一個 @Service 注解。
package io.mieux.service;
import org.springframework.stereotype.Service;
@Service
public class AppService {
}
修改 BeanCofig 類:
package io.mieux.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
@ComponentScan(value = "io.mieux", includeFilters = {@Filter(type = FilterType.ANNOTATION, classes = {Controller.class})})
public class BeanConfig {
}
運行效果:
beanName: org.springframework.context.annotation.internalConfigurationAnnotationProcessor
beanName: org.springframework.context.annotation.internalAutowiredAnnotationProcessor
beanName: org.springframework.context.annotation.internalRequiredAnnotationProcessor
beanName: org.springframework.context.annotation.internalCommonAnnotationProcessor
beanName: org.springframework.context.event.internalEventListenerProcessor
beanName: org.springframework.context.event.internalEventListenerFactory
beanName: beanConfig
beanName: appController
beanName: appService
配置里面,應該是只包含 @Controller 注解的類才會被注冊到容器中,為什么 @Service 注解的類也被注冊了呢?
這里涉及到 @ComponentScan 的一個useDefaultFilters
屬性的用法,該屬性默認值為 true,也就是說 spring 默認會自動發現被 @Component、@Repository、@Service 和 @Controller 標注的類,並注冊進容器中。要達到只包含某些包的掃描效果,就必須將這個默認行為給禁用掉(在 @ComponentScan 中將 useDefaultFilters 設為 false 即可)。
package io.mieux.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
@ComponentScan(value = "io.mieux",
includeFilters = {@Filter(type = FilterType.ANNOTATION, classes = {Controller.class})},
useDefaultFilters = false)
public class BeanConfig {
}
運行效果:
beanName: org.springframework.context.annotation.internalConfigurationAnnotationProcessor
beanName: org.springframework.context.annotation.internalAutowiredAnnotationProcessor
beanName: org.springframework.context.annotation.internalRequiredAnnotationProcessor
beanName: org.springframework.context.annotation.internalCommonAnnotationProcessor
beanName: org.springframework.context.event.internalEventListenerProcessor
beanName: org.springframework.context.event.internalEventListenerFactory
beanName: beanConfig
beanName: appController
添加多種掃描規則
1、如果使用的 jdk8,則可以直接添加多個 @ComponentScan 來添加多個掃描規則,但是在配置類中要加上 @Configuration 注解,否則無效。
package io.mieux.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@ComponentScan(value = "io.mieux.controller")
@ComponentScan(value = "io.mieux.service")
@Configuration
public class BeanConfig {
}
2、也可以使用 @ComponentScans
來添加多個 @ComponentScan,從而實現添加多個掃描規則。同樣,也需要加上 @Configuration 注解,否則無效。
package io.mieux.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
@ComponentScans(value =
{@ComponentScan(value = "io.mieux.controller"),
@ComponentScan(value = "io.mieux.service")})
@Configuration
public class BeanConfig {
}
添加自定義過濾規則
在前面使用過 @Filter 注解,里面的 type 屬性是一個 FilterType 的枚舉類型:
public enum FilterType {
ANNOTATION,
ASSIGNABLE_TYPE,
ASPECTJ,
REGEX,
CUSTOM
}
使用 CUSTOM 類型,就可以實現自定義過濾規則。
1、 首先創建一個實現 TypeFilter 接口的 CustomTypeFilter 類,並實現其 match 方法。
package io.mieux.config;
import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;
import java.io.IOException;
public class CustomTypeFilter implements TypeFilter {
@Override
public boolean match(MetadataReader metadataReader,
MetadataReaderFactory metadataReaderFactory) throws IOException {
// 獲取當前掃描到的類的注解元數據
AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
// 獲取當前掃描到的類的元數據
ClassMetadata classMetadata = metadataReader.getClassMetadata();
// 獲取當前掃描到的類的資源信息
Resource resource = metadataReader.getResource();
if (classMetadata.getClassName().contains("Co")) {
return true;
}
return false;
}
}
這里簡單對掃描到的類名進行判斷,如果類名包含”Co“的就符合條件,也就會注入到容器中。
2、對 BeanConfig 進行修改,指定過濾類型為 Custom 類型,並指定 value 為 CustomTypeFilter.class。
package io.mieux.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
@ComponentScan(value = "io.mieux",
includeFilters = {@ComponentScan.Filter(type = FilterType.CUSTOM, value = {CustomTypeFilter.class})},
useDefaultFilters = false)
public class BeanConfig {
}
運行效果:
beanName: org.springframework.context.annotation.internalConfigurationAnnotationProcessor
beanName: org.springframework.context.annotation.internalAutowiredAnnotationProcessor
beanName: org.springframework.context.annotation.internalRequiredAnnotationProcessor
beanName: org.springframework.context.annotation.internalCommonAnnotationProcessor
beanName: org.springframework.context.event.internalEventListenerProcessor
beanName: org.springframework.context.event.internalEventListenerFactory
beanName: beanConfig
beanName: appController
Springboot配置掃描其它模塊路徑的方法
@SpringBootApplication
=@Configuration
+@EnableAutoConfiguration
+@ComponentScan
,其中掃描包的范圍為啟動類所在包和子包,不包括第三方的jar包。如果我們需要掃描通過maven依賴添加的jar,我們就要單獨使用@ComponentScan注解掃描第三方包。
但是,如果@SpringBootApplication
和@ComponentScan
注解共存,那么@SpringBootApplication
注解的掃描的作用將會失效,也就是說不能夠掃描啟動類所在包以及子包了。因此,我們必須在@ComponentScan注解配置本工程需要掃描的包范圍。
@SpringBootApplication
啟動時會默認掃描主類當前包及子包,如果需要掃描主類當前包以外的其他包,可用如下注解配置實現:
@SpringBootApplication
@ComponentScan(basePackages = {"com.oxing.mall","com.oxing.blog"})
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
注意這里basePackages
必須包含所有的掃描路徑,此時@SpringBootApplication中的@ComponentScan
已經失效
使用spring.factories加載第三方的Bean
在java spring cloud
項目中,我們常常會在子模塊中創建公共類庫,作為驅動包。如果在另外一個子模塊中,需要加載配置文件的時候,往往Spring Boot 自動掃描包的時候,只會掃描自己模塊下的類。
拋出一個問題
首先拋出一個問題:
如果想要被Spring容器管理的Bean的路徑不在Spring Boot 的包掃描路徑下,怎么辦呢?也就是如何去加載第三方的Bean 呢?
有兩種方式可以解決。這里我們使用Swagger的配置來做實驗。
- 首先一個Swagger的配置類:SwaggerConfig
SwaggerConfig 代碼:
@Configuration
@EnableSwagger2
public class SwaggerConfig implements EnvironmentAware {
private static final Logger log = LoggerFactory.getLogger(SwaggerConfig.class);
@Autowired
private Environment env;
@Value("${swagger.scan.package}")
private String swaggerScanPackage;
public SwaggerConfig() {
}
@Bean
public Docket createRestApi() {
Predicate<String> path = PathSelectors.any();
if (Arrays.asList(this.env.getActiveProfiles()).contains("prod")) {
path = PathSelectors.none();
}
log.info("####初始化createRestApi####swaggerScanPackage:" + this.swaggerScanPackage);
log.info(path.toString());
return (new Docket(DocumentationType.SWAGGER_2)).apiInfo(this.apiInfo()).select().apis(RequestHandlerSelectors.basePackage(this.swaggerScanPackage)).paths(PathSelectors.any()).build();
}
private ApiInfo apiInfo() {
log.info("##################################初始化API信息################################################");
return (new ApiInfoBuilder()).title("APIs").description("…………").termsOfServiceUrl("https://js.dazhi.loan.com").version("1.0").build();
}
@Override
public void setEnvironment(Environment environment) {
}
}
- 再看工程結構:
發現我的SwaggerConfig 類和 SpringBoot 的啟動類ConfigApplication.java 不在同一級目錄下,所以當Spring Boot 自動掃描包的時候,是掃描不到我的SwaggerConfig 的配置的,也就在控制台沒有Swagger的打印的信息:
所以這時候我如果想要把SwaggerConfig 加載到Spring容器中的話 要怎么辦呢?下面介紹兩種方式
方法一、在Spring Boot Application 主類上 使用@Import 注解
方法二、創建spring.factories文件
現在我們將其改造一下,采用spring.factories 的方式去加載SwaggerConfig類,在resources目錄下新建一個META-INF 的目錄,然后再新建一個spring.factories 的文件,里面的內容為:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.sg.config.SwaggerConfig
然后在把Spring Boot 啟動類上的@Import注釋掉,啟動發現也可以把SwaggerConfig加載到Spring 容器中
到這就完成了加載一個Spring 不能掃描到的一個類,他可以是第三方的,也可以是自己寫的,只要是Spring Boot 默認掃描路徑不能夠掃描到,都可以使用這種方式去加載!!!