首页 > 其他分享 >一文带你了解 Spring 的@Enablexxx 注解

一文带你了解 Spring 的@Enablexxx 注解

时间:2022-11-27 21:01:08浏览次数:63  
标签:Enablexxx Spring springframework org import Import annotation 注解

layout: post
categories: Java
title: 一文带你了解 Spring 的@Enablexxx 注解
tagline: by 子悠
tags: 
  - 子悠

前面的文章给大家介绍 Spring 的重试机制的时候有提到过 Spring 有很多 @Enable 开头的注解,平时在使用的时候也没有注意过为什么会有这些注解,今天就给大家介绍一下。

@Enable 注解

首先我们先看一下有哪些常用的 @Enable 开头的注解,以及都是干什么用的。

  • @EnableRetry:开启 Spring 的重试功能;
  • @EnableScheduling:开启 Spring 的定时功能;
  • @EnableAsync:开启 Spring 的异步功能;
  • @EnableAutoConfiguration:开启 Spring 的自动装配功能;

上面这几个是我们经常会用到和看到的,都知道在使用相应的功能的时候,如果没有配置上面的注解功能都是不生效的。以我们前面的文章的 Spring 重试为例,我们需要在启动类上面配置 @EnableRetry ,否则自动重试注解 @Retryable 是不会生效的,如下所示,没看过的可以去看下,https://mp.weixin.qq.com/s/U_nm92ujCGArkii5ze7uaA。

@Import 注解

那有的小伙伴就要问了,这个 @EnableRetry 注解到底有什么作用呢?不用这个注解就没办法了吗?

要知道这个注解有什么功效,我们可以点开看看源码,代码如下

package org.springframework.retry.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@EnableAspectJAutoProxy(proxyTargetClass = false)
@Import(RetryConfiguration.class)
@Documented
public @interface EnableRetry {

	boolean proxyTargetClass() default false;
}

可以看到源码很简单,其中最有用的就一行 @Import(RetryConfiguration.class) ,我们可以尝试把这一行代码放到启动类上面看看效果,如下所示,可以看到项目可以正常启动,并且也还是有效果的,说明跟我们的 @EnableRetry 注解是一样的。

从上面的实验效果我们可以看到 @EnableRetry 注解其实就是对 @Import(RetryConfiguration.class) 的一个封装,同样的通过源码我们还可以看到 @EnableScheduling 注解就是对 @Import({SchedulingConfiguration.class}) 的一个封装。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Import({SchedulingConfiguration.class})
@Documented
public @interface EnableScheduling {
}

那如果在没有 @Enablexxx 注解的时候,我们直接通过 @Import 注解是可以这样写的,在一个 @Import 注解里面包含多个配置类,不过这种在配置类较多的场景下还是相对不够简洁的,因而才有了各自功能对应的 @Enable 注解。

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.retry.annotation.RetryConfiguration;
import org.springframework.scheduling.annotation.SchedulingConfiguration;

@SpringBootApplication
@ComponentScan(value = "com.example.demo.*")
@Import({RetryConfiguration.class, SchedulingConfiguration.class})
public class DemoApplication {

  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }

}

为什么要使用 @Import 注解呢

那么很多的小伙伴又要问了,为啥要通过使用 @Import 注解将配置类加载进来呢?在项目中的 Spring 上下文中不是能直接获取到吗?为此我们来实验一下,通过下面的代码我们看下是否能在 Spring 的容器中获取到 RetryConfiguration Bean

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.retry.annotation.RetryConfiguration;
import org.springframework.scheduling.annotation.SchedulingConfiguration;

@SpringBootApplication
@ComponentScan(value = "com.example.demo.*")
//@Import({RetryConfiguration.class, SchedulingConfiguration.class})
public class DemoApplication {

  public static void main(String[] args) {

    ConfigurableApplicationContext applicationContext = SpringApplication.run(DemoApplication.class, args);
    Object bean = applicationContext.getBean("org.springframework.retry.annotation.RetryConfiguration");
    System.out.println(bean.toString());
  }
}

启动过后我们可以看到结果如下,提示我们在容器中找不到这个 bean,有点小伙伴会说是不是 bean 的名字写错了,其实并不是,紧接着我们再把注释的那一行放开再运行一下。

可以看到,这次我们成功的获取到了这个 Bean,这个实验就是告诉我们,其实在默认情况下,Spring 的容器中是找不到RetryConfiguration 这个 Bean 的,因此我们需要通过使用 @Import 注解,将该类加载到容器中。

那么为什么在容器中找不到这个 Bean 呢?

其实很简单,因为这个 Bean 跟我们当前环境的类是不是同一个包里面的,在项目启动的过程中并不会扫描到 RetryConfiguration 类所在的包,因此找不到是很正常的。

总结

上面通过 @EnableRetry 这个注解带大家了解了一下 Spring@Enable 开头的注解的使用原理,相信大家对这些注解有了更深入的了解。简单来说就是因为我们要使用的很多类并不在我们项目所在的包下面,我们不能将所有的依赖包都进行扫描,也不不方便将所有的配置类都通过 @Import 的方式进行导入,而是让每个功能的项目包都提供一个 @Enable 开头的注解,我们直接启用注解就可以达到效果。

这种方式我们在平时的开发中也可以自己实现,实现一个自己的 @Enable 开头的注解来实现特定的功能,下一篇文章我们来带大家实现一下。好了,今天的文章就到这里,如果觉得有帮助还请大家帮我们的文章点赞,评论,转发,一键三连走起。


更多优质内容欢迎关注公众号【Java 极客技术】,我准备了一份面试资料,回复【bbbb07】免费领取。希望能在这寒冷的日子里,帮助到大家。

标签:Enablexxx,Spring,springframework,org,import,Import,annotation,注解
From: https://www.cnblogs.com/zi-you/p/16920365.html

相关文章

  • springBoot mybatis-plus mySql代码生成器
    一、用到的依赖参考官网<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/X......
  • SpringBoot面试题
    1SpringBoot启动Tomcat1.1Spring在启动时创建一个Spring容器1.2利用@ConditionalOnClass技术判断classpath中是否存丰Tomcat依赖,如果存在则生成一个启动Tomcat的Bean1.......
  • Spring Cloud 最新版发布,Spring Security + OAuth2 终于安排上了!
    大家好,我是栈长。今天给大家通报一则框架更新消息,时隔两个月,SpringCloud2021.0.5最新版发布了,来看下最新的SpringCloud版本情况:SpringCloud无疑是现在Java微服务......
  • Spring Boot笔记(全)
    前言serverlet服务器端小程序,第一代javaweb开发技术,基于java实现了一套用于动态网站的APITomcat\Jetty\Undertow都是Servelet容器,用来管理Servelet类jsp在html页......
  • Grafana+OpenSearch+Spring Boot集成(三) 【Grafana警报】
    上一篇:Grafana+OpenSearch+SpringBoot集成(二)【Grafana使用】在数据可视化中,可能会存在监测某项数据是否会超过一定数值的需求。Grafana提供了自动告警功能,可以通过配......
  • Java基础_Spring三种构造方法
    第一种构造方法接口:FirstDaopackagecom.leehl.springgitider.dao;publicinterfaceFirstDao{publicvoidsave();}主方法:FirstDaoimplpacka......
  • SpringCloud
    SpringCloud什么是springcloud?SpringCloud是⼀系列框架的有序集合。它利⽤SpringBoot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中⼼......
  • 第15章-Spring AOP切点表达式(Pointcut)详解
    目录一、概述二、切点表达式配置1.内置配置2.注解配置3.公共配置二、切点表达式类型executionwithinthistargetargsbean@within@target@annotation@args三、切点表达式......
  • 第16章-Spring AOP中的基础API
    目录一、概述二、切点(Pointcut)三、通知(Advice)1.环绕通知2.前置通知3.异常通知4.后置通知四、通知者(Advisor)五、附录1.常用接口2.示例代码前面我们讲了基于XML和注......
  • 什么是SpringBoot
           ......