怎么使用spring中ponentScan自动扫描并指定扫描规则


这篇文章主要介绍了怎么使用spring中ponentScan自动扫描并指定扫描规则的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇怎么使用spring中ponentScan自动扫描并指定扫描规则文章都会有所收获,下面我们一起来看看吧。

1.使用注解配置包扫描

1.1.创建相关类

分别创建BookDao、BookService、BookServiceImpl以及BookController这三个类,并在这三个类中分别添加@Repository、@Service、@Controller注解

BookDao

package&nbsp.tianxia.springannotation.dao;importorg.springframework.stereotype.Repository;/***BookDao*@authorliqb*@date2023-04-2116:37**///名字默认是类名首字母小写@RepositorypublicclassBookDao{}

BookService

package&nbsp.tianxia.springannotation.service;/***BookService*@authorliqb*@date2023-04-2116:38**/publicinterfaceBookService{}

BookServiceImpl

package&nbsp.tianxia.springannotation.service.impl;import&nbsp.tianxia.springannotation.service.BookService;importorg.springframework.stereotype.Service;/***BookServiceImpl*@authorliqb*@date2023-04-2116:38**/@ServicepublicclassBookServiceImplimplementsBookService{}

BookController

package&nbsp.tianxia.springannotation.controller;importorg.springframework.stereotype.Controller;/***BookController*@authorliqb*@date2023-04-2116:39**/@ControllerpublicclassBookController{}

1.2.SpringBoot启动类默认就有配置ponentScan

@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Inherited@SpringBootConfiguration@EnableAutoConfigurationponentScan(excludeFilters={@Filter(type=FilterType.CUSTOM,classes=TypeExcludeFilter.class),@Filter(type=FilterType.CUSTOM,classes=AutoConfigurationExcludeFilter.class)})public@interfaceSpringBootApplication{}

1.3.查看IOC中的bean的名称

package&nbsp.tianxia.springannotation;import&nbsp.tianxia.springannotation.config.MainConfig;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.context.annotation.AnnotationConfigApplicationContext;importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;/***&nbspponentScanTest*@authorliqb*@date2023-04-2116:45**/@SpringBootTest@RunWith(SpringJUnit4ClassRunner.class)publicclass&nbspponentScanTest{/***查看IOC容器中有哪些bean*@authorliqb*@date2023-04-2116:45*/@Testpublicvoidtest(){AnnotationConfigApplicationContextapplicationContext=newAnnotationConfigApplicationContext(SpringAnnotationApplication.class);//我们现在就来看一下IOC容器中有哪些bean,即容器中所有bean定义的名字String[]definitionNames=applicationContext.getBeanDefinitionNames();for(Stringname:definitionNames){System.out.println(name);}}}

2.扫描时排除注解标注的类

在注解类上通过ponentScan注解的excludeFilters()方法

package&nbsp.tianxia.springannotation;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.context.annotationponentScan;importorg.springframework.context.annotation.FilterType;importorg.springframework.stereotype.Controller;importorg.springframework.stereotype.Service;/***启动类*@authorliqb*@date2023-04-2116:12**/@SpringBootApplication//value指定要扫描的包ponentScan(value=&quot.tianxia.springannotation",excludeFilters={/**type:指定你要排除的规则,是按照注解进行排除,还是按照给定的类型进行排除,还是按照正则表达式进行排除,等等*classes:除了@Controller和@Service标注的组件之外,IOC容器中剩下的组件我都要,即相当于是我要排除@Controller和@Service这俩注解标注的组件。*/ponentScan.Filter(type=FilterType.ANNOTATION,classes={Controller.class,Service.class})})publicclassSpringAnnotationApplication{publicstaticvoidmain(String[]args){SpringApplication.run(SpringAnnotationApplication.class,args);}}

3.扫描时只包含注解标注的类

在注解类中的includeFilters()方法来指定Spring在进行包扫描时,只包含哪些注解标注的

这里需要注意的是,当我们使用includeFilters()方法来指定只包含哪些注解标注的类时,需要禁用掉默认的过滤规则

package&nbsp.tianxia.springannotation;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.context.annotationponentScan;importorg.springframework.context.annotation.FilterType;importorg.springframework.stereotype.Controller;/***启动类*@authorliqb*@date2023-04-2116:12**/@SpringBootApplication//value指定要扫描的包ponentScan(value=&quot.tianxia.springannotation",includeFilters={/**type:指定你要排除的规则,是按照注解进行排除,还是按照给定的类型进行排除,还是按照正则表达式进行排除,等等*classes:我们需要Spring在扫描时,只包含@Controller注解标注的类*/ponentScan.Filter(type=FilterType.ANNOTATION,classes={Controller.class})},useDefaultFilters=false)publicclassSpringAnnotationApplication{publicstaticvoidmain(String[]args){SpringApplication.run(SpringAnnotationApplication.class,args);}}

4.重复注解

ponentScans({ponentScan(value=&quot.tianxia.springannotation",includeFilters={/**type:指定你要排除的规则,是按照注解进行排除,还是按照给定的类型进行排除,还是按照正则表达式进行排除,等等*classes:我们需要Spring在扫描时,只包含@Controller注解标注的类*/ponentScan.Filter(type=FilterType.ANNOTATION,classes={Controller.class})},useDefaultFilters=false),ponentScan(value=&quot.tianxia.springannotation",includeFilters={/**type:指定你要排除的规则,是按照注解进行排除,还是按照给定的类型进行排除,还是按照正则表达式进行排除,等等*classes:我们需要Spring在扫描时,只包含@Controller注解标注的类*/ponentScan.Filter(type=FilterType.ANNOTATION,classes={Service.class})},useDefaultFilters=false)})

关于“怎么使用spring中ponentScan自动扫描并指定扫描规则”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“怎么使用spring中ponentScan自动扫描并指定扫描规则”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注主机评测网行业资讯频道。


上一篇:Go语言时间处理必备技巧有哪些

下一篇:python怎么实现一个简单的web应用框架


Copyright © 2002-2019 测速网 www.inhv.cn 皖ICP备2023010105号
测速城市 测速地区 测速街道 网速测试城市 网速测试地区 网速测试街道
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!

热门搜索 城市网站建设 地区网站制作 街道网页设计 大写数字 热点城市 热点地区 热点街道 热点时间 房贷计算器