意见箱
恒创运营部门将仔细参阅您的意见和建议,必要时将通过预留邮箱与您保持联络。感谢您的支持!
意见/建议
提交建议

详解: Spring 的 @Enable 开头

来源:恒创科技 编辑:恒创科技编辑部
2024-01-28 12:18:59
@Enable 注解

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

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

上面这几个是我们经常会用到和看到的,都知道在使用相应的功能的时候,如果没有配置上面的注解功能都是不生效的。以我们前面的文章的Spring重试为例,我们需要在启动类上面配置@EnableRetry,否则自动重试注解@Retryable是不会生效的,如下所示,没看过的可以去看下,Java 远程调用失败?如何优雅的进行重试?。

详解: Spring 的 @Enable 开头_java


详解: Spring 的 @Enable 开头

@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注解是一样的。

详解: Spring 的 @Enable 开头_java_02

从上面的实验效果我们可以看到@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的名字写错了,其实并不是,紧接着我们再把注释的那一行放开再运行一下。

详解: Spring 的 @Enable 开头_封装_03

详解: Spring 的 @Enable 开头_java_04

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

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

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

总结

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

这种方式我们在平时的开发中也可以自己实现,实现一个自己的@Enable开头的注解来实现特定的功能。

上一篇: 聊一聊Java并发运行中的一些安全问题 下一篇: 手机怎么远程登录云服务器?