温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2023-04-28 16:24:33 来源:亿速云 阅读:124 作者:iii 栏目:开发技术

这篇文章主要介绍了怎么使用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);
        }
    }
}

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

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);
    }
}

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

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);
    }
}

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

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自动扫描并指定扫描规则”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI