javascript
Spring注解——使用@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 這個類注冊進容器中了。
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: appControllerAppController 已經被注冊進容器了。
4.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: appController5.添加多種掃描規則
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 {}6.添加自定義過濾規則
在前面使用過 @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 {@Overridepublic 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總結
以上是生活随笔為你收集整理的Spring注解——使用@ComponentScan自动扫描组件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python3的3D实战 -基于pand
- 下一篇: python3的3D开发-基于blend