一文详解Spring的Enablexxx注解使用实例
作者:zi-you 时间:2023-09-03 08:43:18
引言
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
是不会生效的
@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
类所在的包,因此找不到是很正常的。
来源:https://www.cnblogs.com/zi-you/p/16920365.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringMvc web.xml配置实现原理过程解析
Java中的"goto"语句妙用
![](https://img.aspxhome.com/file/2023/3/78853_0s.png)
JAVA中的静态代理、动态代理以及CGLIB动态代理总结
![](https://img.aspxhome.com/file/2023/4/122014_0s.png)
详解使用Spring Security进行自动登录验证
![](https://img.aspxhome.com/file/2023/8/72288_0s.jpg)
Java初学者问题图解(动力节点Java学院整理)
![](https://img.aspxhome.com/file/2023/2/128932_0s.png)
c# 用ICSharpCode组件压缩文件
c# 开发语音识别程序
![](https://img.aspxhome.com/file/2023/3/91313_0s.png)
Java 17的一些新特性介绍
Java 中的弱引用是什么
Android视频悬浮窗口实现的示例代码
![](https://img.aspxhome.com/file/2023/6/138386_0s.gif)
协定需要会话,但是绑定“BasicHttpBinding”不支持它或者因配置不正确而无法支持它
mybatis createcriteria和or的区别说明
Java中的Object类详细介绍
springboot自定义过滤器的方法
Java求字符串中出现次数最多的字符串以及出现次数
Java经典面试题最全汇总208道(二)
![](https://img.aspxhome.com/file/2023/7/59067_0s.jpg)
C#引用类型和值类型的介绍与实例
![](https://img.aspxhome.com/file/2023/7/100087_0s.jpg)
从字符串中截取等长字节的Java代码
Java拦截器Interceptor和过滤器Filte的执行顺序和区别
![](https://img.aspxhome.com/file/2023/2/61142_0s.jpg)
Java中的clone方法详解_动力节点Java学院整理
![](https://img.aspxhome.com/file/2023/6/116246_0s.png)