这篇文章主要介绍了怎么使用spring中@ComponentScan自动扫描并指定扫描规则的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇怎么使用spring中@ComponentScan自动扫描并指定扫描规则文章都会有所收获,下面我们一起来看看吧。
1.使用注解配置包扫描
1.1.创建相关类
分别创建BookDao、BookService、BookServiceImpl以及BookController这三个类,并在这三个类中分别添加@Repository、@Service、@Controller注解
BookDao
package com.tianxia.springannotation.dao; import org.springframework.stereotype.Repository; /** * BookDao * @author liqb * @date 2023-04-21 16:37 **/ // 名字默认是类名首字母小写 @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启动类默认就有配置@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); // 我们现在就来看一下IOC容器中有哪些bean,即容器中所有bean定义的名字 String[] definitionNames = applicationContext.getBeanDefinitionNames(); for (String name : definitionNames) { System.out.println(name); } } }
2.扫描时排除注解标注的类
在注解类上通过@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; /** * 启动类 * @author liqb * @date 2023-04-21 16:12 **/ @SpringBootApplication // value指定要扫描的包 @ComponentScan(value="com.tianxia.springannotation", excludeFilters={ /* * type:指定你要排除的规则,是按照注解进行排除,还是按照给定的类型进行排除,还是按照正则表达式进行排除,等等 * classes:除了@Controller和@Service标注的组件之外,IOC容器中剩下的组件我都要,即相当于是我要排除@Controller和@Service这俩注解标注的组件。 */ @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.扫描时只包含注解标注的类
在注解类中的includeFilters()方法来指定Spring在进行包扫描时,只包含哪些注解标注的
这里需要注意的是,当我们使用includeFilters()方法来指定只包含哪些注解标注的类时,需要禁用掉默认的过滤规则
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; /** * 启动类 * @author liqb * @date 2023-04-21 16:12 **/ @SpringBootApplication // value指定要扫描的包 @ComponentScan(value="com.tianxia.springannotation", includeFilters={ /* * type:指定你要排除的规则,是按照注解进行排除,还是按照给定的类型进行排除,还是按照正则表达式进行排除,等等 * classes:我们需要Spring在扫描时,只包含@Controller注解标注的类 */ @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.重复注解
@ComponentScans({ @ComponentScan(value="com.tianxia.springannotation", includeFilters={ /* * type:指定你要排除的规则,是按照注解进行排除,还是按照给定的类型进行排除,还是按照正则表达式进行排除,等等 * classes:我们需要Spring在扫描时,只包含@Controller注解标注的类 */ @ComponentScan.Filter(type=FilterType.ANNOTATION, classes={Controller.class}) }, useDefaultFilters=false), @ComponentScan(value="com.tianxia.springannotation", includeFilters={ /* * type:指定你要排除的规则,是按照注解进行排除,还是按照给定的类型进行排除,还是按照正则表达式进行排除,等等 * classes:我们需要Spring在扫描时,只包含@Controller注解标注的类 */ @ComponentScan.Filter(type=FilterType.ANNOTATION, classes={Service.class}) }, useDefaultFilters=false) })