spring中@ComponentScan自動(dòng)掃描并指定掃描規(guī)則
1.使用注解配置包掃描
1.1.創(chuàng)建相關(guān)類
分別創(chuàng)建BookDao、BookService、BookServiceImpl以及BookController這三個(gè)類,并在這三個(gè)類中分別添加@Repository、@Service、@Controller注解
BookDao
package com.tianxia.springannotation.dao; import org.springframework.stereotype.Repository; /** * BookDao * @author liqb * @date 2023-04-21 16:37 **/ // 名字默認(rèn)是類名首字母小寫 @Repository public class BookDao { }
BookService
package com.tianxia.springannotation.service; /** * BookService * @author liqb * @date 2023-04-21 16:38 **/ public interface BookService { }
BookServiceImpl
package com.tianxia.springannotation.service.impl; import com.tianxia.springannotation.service.BookService; import org.springframework.stereotype.Service; /** * BookServiceImpl * @author liqb * @date 2023-04-21 16:38 **/ @Service public class BookServiceImpl implements BookService { }
BookController
package com.tianxia.springannotation.controller; import org.springframework.stereotype.Controller; /** * BookController * @author liqb * @date 2023-04-21 16:39 **/ @Controller public class BookController { }
1.2.SpringBoot啟動(dòng)類默認(rèn)就有配置@ComponentScan
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) public @interface SpringBootApplication { }
1.3.查看IOC中的bean的名稱
package com.tianxia.springannotation; import com.tianxia.springannotation.config.MainConfig; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * ComponentScanTest * @author liqb * @date 2023-04-21 16:45 **/ @SpringBootTest @RunWith(SpringJUnit4ClassRunner.class) public class ComponentScanTest { /** * 查看IOC容器中有哪些bean * @author liqb * @date 2023-04-21 16:45 */ @Test public void test() { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringAnnotationApplication.class); // 我們現(xiàn)在就來看一下IOC容器中有哪些bean,即容器中所有bean定義的名字 String[] definitionNames = applicationContext.getBeanDefinitionNames(); for (String name : definitionNames) { System.out.println(name); } } }
2.掃描時(shí)排除注解標(biāo)注的類
在注解類上通過@ComponentScan注解的excludeFilters()方法
package com.tianxia.springannotation; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.FilterType; import org.springframework.stereotype.Controller; import org.springframework.stereotype.Service; /** * 啟動(dòng)類 * @author liqb * @date 2023-04-21 16:12 **/ @SpringBootApplication // value指定要掃描的包 @ComponentScan(value="com.tianxia.springannotation", excludeFilters={ /* * type:指定你要排除的規(guī)則,是按照注解進(jìn)行排除,還是按照給定的類型進(jìn)行排除,還是按照正則表達(dá)式進(jìn)行排除,等等 * classes:除了@Controller和@Service標(biāo)注的組件之外,IOC容器中剩下的組件我都要,即相當(dāng)于是我要排除@Controller和@Service這倆注解標(biāo)注的組件。 */ @ComponentScan.Filter(type= FilterType.ANNOTATION, classes={Controller.class, Service.class}) }) public class SpringAnnotationApplication { public static void main(String[] args) { SpringApplication.run(SpringAnnotationApplication.class, args); } }
3.掃描時(shí)只包含注解標(biāo)注的類
在注解類中的includeFilters()方法來指定Spring在進(jìn)行包掃描時(shí),只包含哪些注解標(biāo)注的
這里需要注意的是,當(dāng)我們使用includeFilters()方法來指定只包含哪些注解標(biāo)注的類時(shí),需要禁用掉默認(rèn)的過濾規(guī)則
package com.tianxia.springannotation; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.FilterType; import org.springframework.stereotype.Controller; /** * 啟動(dòng)類 * @author liqb * @date 2023-04-21 16:12 **/ @SpringBootApplication // value指定要掃描的包 @ComponentScan(value="com.tianxia.springannotation", includeFilters={ /* * type:指定你要排除的規(guī)則,是按照注解進(jìn)行排除,還是按照給定的類型進(jìn)行排除,還是按照正則表達(dá)式進(jìn)行排除,等等 * classes:我們需要Spring在掃描時(shí),只包含@Controller注解標(biāo)注的類 */ @ComponentScan.Filter(type=FilterType.ANNOTATION, classes={Controller.class}) }, useDefaultFilters=false) public class SpringAnnotationApplication { public static void main(String[] args) { SpringApplication.run(SpringAnnotationApplication.class, args); } }
4.重復(fù)注解
@ComponentScans({ @ComponentScan(value="com.tianxia.springannotation", includeFilters={ /* * type:指定你要排除的規(guī)則,是按照注解進(jìn)行排除,還是按照給定的類型進(jìn)行排除,還是按照正則表達(dá)式進(jìn)行排除,等等 * classes:我們需要Spring在掃描時(shí),只包含@Controller注解標(biāo)注的類 */ @ComponentScan.Filter(type=FilterType.ANNOTATION, classes={Controller.class}) }, useDefaultFilters=false), @ComponentScan(value="com.tianxia.springannotation", includeFilters={ /* * type:指定你要排除的規(guī)則,是按照注解進(jìn)行排除,還是按照給定的類型進(jìn)行排除,還是按照正則表達(dá)式進(jìn)行排除,等等 * classes:我們需要Spring在掃描時(shí),只包含@Controller注解標(biāo)注的類 */ @ComponentScan.Filter(type=FilterType.ANNOTATION, classes={Service.class}) }, useDefaultFilters=false) })
到此這篇關(guān)于spring中@ComponentScan自動(dòng)掃描并指定掃描規(guī)則的文章就介紹到這了,更多相關(guān)spring @ComponentScan自動(dòng)掃描內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Jackson忽略字段實(shí)現(xiàn)對(duì)字段進(jìn)行序列化和反序列化
在使用?Jackson?進(jìn)行序列化和反序列化時(shí),有時(shí)候需要對(duì)某些字段進(jìn)行過濾,以便在?JSON?數(shù)據(jù)中不包含某些敏感信息,下面就一起來了解一下Jackson忽略字段實(shí)現(xiàn)對(duì)字段進(jìn)行序列化和反序2023-10-10SpringBoot 配合 SpringSecurity 實(shí)現(xiàn)自動(dòng)登錄功能的代碼
這篇文章主要介紹了SpringBoot 配合 SpringSecurity 實(shí)現(xiàn)自動(dòng)登錄功能的代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09Java編程實(shí)現(xiàn)調(diào)用com操作Word方法實(shí)例代碼
這篇文章主要介紹了Java編程實(shí)現(xiàn)調(diào)用com操作Word方法實(shí)例代碼,代碼注釋很詳細(xì),在這里分給大家,需要的朋友可以參考下。2017-09-09詳解Java關(guān)于JDK中時(shí)間日期的API
這篇文章主要介紹了詳解Java關(guān)于JDK中時(shí)間日期的API,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09SpringBoot整合atomikos實(shí)現(xiàn)跨庫事務(wù)的詳細(xì)方案
這篇文章主要介紹了SpringBoot整合atomikos實(shí)現(xiàn)跨庫事務(wù),業(yè)務(wù)主要涉及政府及企業(yè)且并發(fā)量不大,所以采用XA事務(wù),雖然性能有所損失,但是可以保證數(shù)據(jù)的強(qiáng)一致性,需要的朋友可以參考下2022-06-06踩坑之spring事務(wù),非事務(wù)方法與事務(wù)方法執(zhí)行相互調(diào)用方式
這篇文章主要介紹了踩坑之spring事務(wù),非事務(wù)方法與事務(wù)方法執(zhí)行相互調(diào)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07