无需手动注册:精通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 {
  // ...
}



相关文章
|
5天前
|
Java API 数据安全/隐私保护
掌握Spring Boot中的@Validated注解
【4月更文挑战第23天】在 Spring Boot 开发中,@Validated 注解是用于开启和利用 Spring 的验证框架的一种方式,特别是在处理控制层的输入验证时。本篇技术博客将详细介绍 @Validated 注解的概念和使用方法,并通过实际的应用示例来展示如何在项目中实现有效的数据验证
26 3
|
5天前
|
前端开发 Java 开发者
深入理解Spring Boot中的@Service注解
【4月更文挑战第22天】在 Spring Boot 应用开发中,@Service 注解扮演着特定的角色,主要用于标识服务层组件。本篇技术博客将全面探讨 @Service 注解的概念,并提供实际的应用示例,帮助开发者理解如何有效地使用这一注解来优化应用的服务层架构
110 1
|
5天前
|
Java 开发者 Spring
深入理解Spring Boot的@ComponentScan注解
【4月更文挑战第22天】在构建 Spring Boot 应用时,@ComponentScan 是一个不可或缺的工具,它使得组件发现变得自动化和高效。这篇博客将详细介绍 @ComponentScan 的基本概念、关键属性及其在实际开发中的应用。
33 4
|
5天前
|
Java 开发者 Spring
深入理解 Spring Boot 中的 @EnableAutoConfiguration 注解:概念与实践
【4月更文挑战第21天】在 Spring Boot 项目中,@EnableAutoConfiguration 注解是实现自动配置的核心,它可以根据项目的依赖和配置,自动地配置 Spring 应用程序中的 Bean
37 3
|
5天前
|
缓存 Java Sentinel
Springboot 中使用 Redisson+AOP+自定义注解 实现访问限流与黑名单拦截
Springboot 中使用 Redisson+AOP+自定义注解 实现访问限流与黑名单拦截
|
5天前
|
运维 Java 程序员
Spring5深入浅出篇:基于注解实现的AOP
# Spring5 AOP 深入理解:注解实现 本文介绍了基于注解的AOP编程步骤,包括原始对象、额外功能、切点和组装切面。步骤1-3旨在构建切面,与传统AOP相似。示例代码展示了如何使用`@Around`定义切面和执行逻辑。配置中,通过`@Aspect`和`@Around`注解定义切点,并在Spring配置中启用AOP自动代理。 进一步讨论了切点复用,避免重复代码以提高代码维护性。通过`@Pointcut`定义通用切点表达式,然后在多个通知中引用。此外,解释了AOP底层实现的两种动态代理方式:JDK动态代理和Cglib字节码增强,默认使用JDK,可通过配置切换到Cglib
|
4天前
|
Java
Springboot 使用自定义注解结合AOP方式校验接口参数
Springboot 使用自定义注解结合AOP方式校验接口参数
Springboot 使用自定义注解结合AOP方式校验接口参数
|
5天前
|
存储 缓存 Java
【JavaEE】Spring中注解的方式去获取Bean对象
【JavaEE】Spring中注解的方式去获取Bean对象
3 0
|
5天前
|
存储 Java 对象存储
【JavaEE】Spring中注解的方式去存储Bean对象
【JavaEE】Spring中注解的方式去存储Bean对象
9 0
|
5天前
|
JSON 前端开发 Java
【JAVA进阶篇教学】第七篇:Spring中常用注解
【JAVA进阶篇教学】第七篇:Spring中常用注解