iis服务器助手广告
返回顶部
首页 > 资讯 > 后端开发 > Python >spring中@ComponentScan自动扫描并指定扫描规则
  • 126
分享到

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

@ComponentScan自动扫描spring@ComponentScan扫描 2023-05-18 05:05:04 126人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录1.使用注解配置包扫描1.1.创建相关类1.2.SpringBoot启动类默认就有配置@ComponentScan1.3.查看ioc中的bean的名称2.扫描时排除注解标注的类3

1.使用注解配置包扫描

1.1.创建相关类

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

BookDao

package com.tianxia.springannotation.dao;
import org.springframework.stereotype.Repository;

// 名字默认是类名首字母小写
@Repository
public class BookDao {
}

BookService

package com.tianxia.springannotation.service;

public interface BookService {
}

BookServiceImpl

package com.tianxia.springannotation.service.impl;
import com.tianxia.springannotation.service.BookService;
import org.springframework.stereotype.Service;

@Service
public class BookServiceImpl implements BookService {
}

BookController

package com.tianxia.springannotation.controller;
import org.springframework.stereotype.Controller;

@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;

@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class ComponentScanTest {
    
    @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;

@SpringBootApplication
// value指定要扫描的包
@ComponentScan(value="com.tianxia.springannotation", excludeFilters={
        
        @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;

@SpringBootApplication
// value指定要扫描的包
@ComponentScan(value="com.tianxia.springannotation", includeFilters={
        
        @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={
                
                @ComponentScan.Filter(type=FilterType.ANNOTATION, classes={Controller.class})
        }, useDefaultFilters=false),
        @ComponentScan(value="com.tianxia.springannotation", includeFilters={
                
                @ComponentScan.Filter(type=FilterType.ANNOTATION, classes={Service.class})
        }, useDefaultFilters=false)
})

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

--结束END--

本文标题: spring中@ComponentScan自动扫描并指定扫描规则

本文链接: https://www.lsjlt.com/news/211741.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作