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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

【Spring注解系列02】@CompentScan与@CompentScans

發布時間:2025/3/20 javascript 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Spring注解系列02】@CompentScan与@CompentScans 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.@CompentScan與@CompentScans

@CompentScan

為掃描包注解,只能作用于類上,一個類上可以有多個@CompentScan注解。

等價于 <context:component-scan base-package="com.java" ></context:component-scan>

使用了@ComponentScan注解后,指定包下的@Controller,@Service,@Repository,@Compent才會生效。

@component注解屬性說明:

//excludeFilters = Filter[] :指定掃描的時候按照什么規則排除那些組件 //includeFilters = Filter[] :指定掃描的時候只需要包含哪些組件 //FilterType.ANNOTATION:按照注解 //FilterType.ASSIGNABLE_TYPE:按照給定的類型; //FilterType.ASPECTJ:使用ASPECTJ表達式 //FilterType.REGEX:使用正則指定 //FilterType.CUSTOM:使用自定義規則

特別注意:過濾器必須實現TypeFilter接口,同時要使過濾器生效,必須將useDefaultFilters設置為false,該屬性默認為true。

<context:component-scan base-package="com.java" use-default-filters="false"></context:component-scan>@ComponentScan(basePackages = {"com.java"},useDefaultFilters = false,includeFilters = {? @ComponentScan.Filter(type = FilterType.ANNOTATION,classes ={Controller.class})?})

@CompentScans

存在多個@ComponentScan時,可以使用@ComponentScans將這些@ComponentScan放在里面統一管理,@ComponentScans是一個數組。

@Configuration @ComponentScan(basePackages = {"com.java.dao"}) @ComponentScan(basePackages = {"com.java.service"}) public class BeanConfig {@Bean("personAlias")public Person person2(){return new Person("2222","lisi");} }//等價@Configuration @ComponentScans({ @ComponentScan(basePackages = {"com.java.dao"}),@ComponentScan(basePackages = {"com.java.service"}) }) public class BeanConfig {@Bean("personAlias")public Person person2(){return new Person("2222","lisi");} }

?

2.實例

條件:在包中已經建立有controller/service/dao的各層類,并且標有對應注解。

@Configuration @ComponentScans({@ComponentScan(basePackages = {"com.java"},useDefaultFilters = false,includeFilters = {//只有Controller注解才能注進來@ComponentScan.Filter(type = FilterType.ANNOTATION,classes ={Controller.class}),//只有符合自定義規則的類才能注進來@ComponentScan.Filter(type = FilterType.CUSTOM,classes ={MyTypeFilter.class})}) }) public class BeanConfig {@Bean("personAlias")public Person person2(){return new Person("2222","lisi");} }

過濾器:過濾器必須實現TypeFilter接口

public class MyTypeFilter implements TypeFilter {/*** metadataReader:讀取到的當前正在掃描的類的信息* metadataReaderFactory:可以獲取到其他任何類信息的*/@Overridepublic boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {//獲取當前類注解的信息AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();System.out.println(annotationMetadata);if(annotationMetadata.getClassName().contains("vice")){return true;}//獲取當前正在掃描的類的類信息ClassMetadata classMetadata = metadataReader.getClassMetadata();//獲取當前類資源(類的路徑)Resource resource = metadataReader.getResource();//獲取任何類,可以是接口,可以是子類MetadataReader service = metadataReaderFactory.getMetadataReader("com.java.dao.PersonDao");return false;} }

測試類

public class BeanDependencyInjectionTest {public static void main(String[] args) {System.out.println("-----------------------------annotation 注解方式注入-----------------------------------");//annotation 注解方式注入ApplicationContext applicationContext1 = new AnnotationConfigApplicationContext(BeanConfig.class);Person person1 = applicationContext1.getBean(Person.class);System.out.println("person1---->"+person1);//獲取所有注入Bean的名稱String[] beanDefinitionNames1 = applicationContext1.getBeanDefinitionNames();for (String name : beanDefinitionNames1) {System.out.println(name);}} }

測試結果:我們可以看到只有controller和Service才能注入,dao是注入不進來的。

-----------------------------annotation 注解方式注入----------------------------------- org.springframework.core.type.classreading.AnnotationMetadataReadingVisitor@382db087 org.springframework.core.type.classreading.AnnotationMetadataReadingVisitor@15bfd87 org.springframework.core.type.classreading.AnnotationMetadataReadingVisitor@543e710e org.springframework.core.type.classreading.AnnotationMetadataReadingVisitor@57f23557 org.springframework.core.type.classreading.AnnotationMetadataReadingVisitor@3d0f8e03 org.springframework.core.type.classreading.AnnotationMetadataReadingVisitor@6366ebe0 person1---->Person{id='2222', name='lisi'} org.springframework.context.annotation.internalConfigurationAnnotationProcessor org.springframework.context.annotation.internalAutowiredAnnotationProcessor org.springframework.context.annotation.internalCommonAnnotationProcessor org.springframework.context.event.internalEventListenerProcessor org.springframework.context.event.internalEventListenerFactory beanConfig personController personService personAlias

?

?

?

總結

以上是生活随笔為你收集整理的【Spring注解系列02】@CompentScan与@CompentScans的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。