无需手动注册:精通Spring注解扫描的高效利用

简介: 无需手动注册:精通Spring注解扫描的高效利用


注解扫描

Spring 框架中,注解扫描(Annotation Scanning)是一种自动发现和注册带有特定注解的组件的机制。通过注解扫描,Spring 可以自动将带有特定注解的类注册为 Bean,并将其纳入到应用程序上下文中进行管理。

要配置注解扫描,可以使用 @ComponentScan 注解或通过 XML 配置文件进行配置。

XML 方式

我们需要再 Spring 的配置文件中引入 context 命名空间,然后使用 标签进行扫描的配置。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 配置扫描包 -->
    <context:component-scan base-package="world.xuewei, world.zhangsan" use-default-filters="true">
        
    </context:component-scan>
</beans>

我们需要指定 base-package 属性,配置需要扫描的包路径(多个包用 , 分割),这样默认扫描策略就会扫描对应包及其子包下的 Bean。use-default-filters 属性表示是否开启默认扫描策略,默认为 true 表示开启。

排除方式

我们可以在 内通过 标签配置排除策略,配置排除策略需要我们开启默认的扫描策略,即 use-default-filters="true"

可以通过为标签的 type 属性指定策略,expression 属性指定对应策略的表达式内容。Spring 提供了五种策略可以使用:

  1. assignable:指定某一类型加入策略。expression 需要指定为某个类的全限定类名。
  2. annotation:指定某一注解加入策略,标记此注解的类都进行策略决策。expression 需要指定为某个注解类的全限定类名。
  3. aspectj:指定切入点表达式加入策略(只包含包名和类名部分),expression 值为切入点表达式。
  4. regex:指定正则表达式加入策略,expression 值为正则表达式。
  5. custom:指定自定义过滤器加入策略,expression 值过滤器类的 Class。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 配置扫描包 -->
    <context:component-scan base-package="world.xuewei, world.zhangsan" use-default-filters="true">
        <context:exclude-filter type="assignable" expression="world.xuewei.entity.Account"/>
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
        <context:exclude-filter type="aspectj" expression="world.xuewei.service..*"/>
        <context:exclude-filter type="regex" expression=".*Test"/>
        <context:exclude-filter type="custom" expression="world.xuewei.filter.MyCustomFilter"/>
    </context:component-scan>
</beans>

包含方式

我们可以在 内通过 标签配置包含策略,配置包含策略需要我们关闭默认的扫描策略,即 use-default-filters="false"

可以通过为标签的 type 属性指定策略,expression 属性指定对应策略的表达式内容。Spring 提供了五种策略可以使用:

  1. assignable:指定某一类型加入策略。expression 需要指定为某个类的全限定类名。
  2. annotation:指定某一注解加入策略,标记此注解的类都进行策略决策。expression 需要指定为某个注解类的全限定类名。
  3. aspectj:指定切入点表达式加入策略(只包含包名和类名部分),expression 值为切入点表达式。
  4. regex:指定正则表达式加入策略,expression 值为正则表达式。
  5. custom:指定自定义过滤器加入策略,expression 值过滤器类的 Class。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 配置扫描包 -->
    <context:component-scan base-package="world.xuewei, world.zhangsan" use-default-filters="false">
        <context:include-filter type="assignable" expression="world.xuewei.entity.Account"/>
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
        <context:include-filter type="aspectj" expression="world.xuewei.service..*"/>
        <context:include-filter type="regex" expression=".*Test"/>
        <context:include-filter type="custom" expression="world.xuewei.filter.MyCustomFilter"/>
    </context:component-scan>
</beans>

包含方式与排除方式需要互斥使用

注解方式

@ComponentScan 注解是 Spring 提供的一种基于注解的配置方式,它可以在 Java 类上直接使用,也可以在配置类上使用。

@ComponentScan 注解包含几个重要的属性:basePackagesexcludeFiltersincludeFiltersuseDefaultFilters。作用和上述 XML 方式一样。

@ComponentScan(basePackages = "world.xuewei", excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = Account.class),
        @ComponentScan.Filter(type = FilterType.ANNOTATION, value = Service.class),
        @ComponentScan.Filter(type = FilterType.REGEX, pattern = ".*Test"),
        @ComponentScan.Filter(type = FilterType.ASPECTJ, pattern = "world.xuewei.dao..*"),
        @ComponentScan.Filter(type = FilterType.CUSTOM, classes = MyCustomFilter.class),
})
@Component
public class AnnotationTest {
  // ...
}



相关文章
|
3月前
|
负载均衡 Java API
|
4月前
|
Java Maven
@ConditionalOnMissingBean 如何实现覆盖第三方组件中的 Bean
@ConditionalOnMissingBean 如何实现覆盖第三方组件中的 Bean
21 0
|
4月前
|
Java Maven
@ConditionalOnMissingBean 如何实现覆盖第三方组件中的 Bean
@ConditionalOnMissingBean 如何实现覆盖第三方组件中的 Bean
17 0
|
5月前
|
Java 编译器 Spring
[Spring 企业应用加速小技巧]将bean扫描提前到编译器,加快应用启动速度
[Spring 企业应用加速小技巧]将bean扫描提前到编译器,加快应用启动速度
|
9月前
|
XML 前端开发 数据库
若依框架如何开启注册功能?
若依框架如何开启注册功能?
1245 0
|
11月前
|
安全 Java 程序员
SpringBoot中如何实现业务校验,这种方式才叫优雅!
SpringBoot中如何实现业务校验,这种方式才叫优雅!
131 0
|
11月前
|
前端开发 Java 数据库
SpringBoot之自动配置类的解析和过滤机制
1.提炼三句话 整体来讲Spring Boot是通过条件注解、条件评估器和自动配置导入器等机制来实现自动配置的。 条件评估器来判断是否需要加载某个自动配置类。条件评估器通常被定义在“org.springframework.boot.autoconfigure.condition”包中,例如,ClassCondition、BeanCondition、MissingBeanCondition、WebApplicationCondition等 条件注解来判断是否需要加载某个自动配置类。条件注解通常被定义在“org.springframework.boot.autoconfigure.conditi
117 0
|
JSON 前端开发 Java
无需额外注解的 SpringBoot API文档生成工具介绍-japidocs
轻松生成api接口文档,使用pandoc导出word接口文档,应用了https://japidocs.agilestudio.cn/的说明
188 0
|
JSON Java 数据库
SpringBoot2.x基础篇:灵活的使用外部化配置信息
`SpringBoot`提供了内部配置`application.yml`文件的方式来进行全局配置,还支持使用`profiles`来激活不同环境下使用不同的配置文件,而这种方式毕竟是已经打包完成了,因此存在一定的局限性,像数据库特殊敏感配置也可能存在泄露的风险,如何解决这种问题呢?我们来看看本章要讲到的外部配置的方式吧!!!
|
XML 开发框架 监控
SpringBoot快速搭建、自动流程进阶、装配机制、功能扩展点详解
Spring Boot启动 Spring Boot是Spring旗下的一个子项目,其设计目的是简化Spring应用的初始搭建及开发过程,Spring Boot可以快速启动和运行你的Spring应用服务。 Spring Boot概述 Spring Boot本质上是基于Spring内核的一个快速开发框架,是“约定优先于配置”理念下的最佳实践,通过解析Spring Boot的启动过程,可以帮助我们逐渐了解它的工作机制和其背后整合Spring快速开发的实现原理。
388 0
SpringBoot快速搭建、自动流程进阶、装配机制、功能扩展点详解