spring中@ComponentScan自动扫描并指定扫描规则

 更新时间:2023年04月28日 08:32:55   作者:怀梦  
本文主要介绍了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)
})

到此这篇关于spring中@ComponentScan自动扫描并指定扫描规则的文章就介绍到这了,更多相关spring @ComponentScan自动扫描内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • MyBatis延迟加载与立即加载案例教程

    MyBatis延迟加载与立即加载案例教程

    这篇文章主要介绍了MyBatis延迟加载与立即加载案例教程,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • Java注解使用及原理解析

    Java注解使用及原理解析

    这篇文章主要介绍了Java注解使用及原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • Java编写实现九宫格应用

    Java编写实现九宫格应用

    这篇文章主要为大家详细介绍了Java编写实现九宫格应用,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • java内存管理关系及内存泄露的原理分析

    java内存管理关系及内存泄露的原理分析

    这篇文章主要介绍了java内存管理关系及内存泄露的原理,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • SpringCloud Zuul自定义filter代码实例

    SpringCloud Zuul自定义filter代码实例

    这篇文章主要介绍了SpringCloud Zuul自定义filter代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • 使用json字符串插入节点或者覆盖节点

    使用json字符串插入节点或者覆盖节点

    这篇文章主要介绍了使用json字符串插入节点或者覆盖节点的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • IDEA 2020.2 +Gradle 6.6.1 + Spring Boot 2.3.4 创建多模块项目的超详细教程

    IDEA 2020.2 +Gradle 6.6.1 + Spring Boot 2.3.4 创建多模块项目的超详细教程

    这篇文章主要介绍了IDEA 2020.2 +Gradle 6.6.1 + Spring Boot 2.3.4 创建多模块项目的教程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-09-09
  • @Autowired注解以及失效的几个原因图文详解

    @Autowired注解以及失效的几个原因图文详解

    在微服务项目中,会遇到@Autowired注解失效的情况,下面这篇文章主要给大家介绍了关于@Autowired注解以及失效的几个原因的相关资料,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2023-03-03
  • 简述mybatis大于小于的转义

    简述mybatis大于小于的转义

    这篇文章主要介绍了mybatis大于小于的转义以及xml中常用转义字符,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-09-09
  • Java字节码ByteBuddy使用及原理解析下

    Java字节码ByteBuddy使用及原理解析下

    这篇文章主要为大家介绍了Java字节码ByteBuddy使用及原理解析下篇,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-05-05

最新评论