【SpringBoot】@Import 注解的使用的三种方式

本文详细介绍了SpringBoot中@Import注解的使用,包括直接导入普通Java类、自定义注解导入以及使用ImportSelector和ImportBeanDefinitionRegistrar动态注册Bean。通过示例展示了如何通过这些方式将Bean注入到Spring容器中。

SpringBoot @Import注解 使用

作用

一句话概括:向Spring注入Bean

三种使用方式

  • 导入普通的 Java 类
  • 自定义的 ImportSelector
  • ImportBeanDefinitionRegistrar

导入普通的java类

import + Configuration 实现注入

还是请我们的User.java 出场吧,定一个User类

public class User {
}

定义UserConfig.java

@Import({User.class})
@Configuration
public class UserConfig {

}

测试

依旧还是修改启动类的代码,通过上下文去获取,这里是根据类型去获取的哈,通过名称的话是获取不到的,不知道注入的bean的名称。

@SpringBootApplication
public class BootImportsApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(BootImportsApplication.class, args);
        Object user = context.getBean(User.class);
        System.out.println(user);
    }

}

输出结果:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.1.RELEASE)

2023-07-03 14:00:34.105  INFO 14848 --- [           main] xx.imports.BootImportsApplication        : Starting BootImportsApplication on ZJZL-20210402GJ with PID 14848 (F:\idea-project\xy-integration-middleware\boot\boot-imports\target\classes started by Administrator in F:\idea-project\xy-integration-middleware)
2023-07-03 14:00:34.118  INFO 14848 --- [           main] xx.imports.BootImportsApplication        : No active profile set, falling back to default profiles: default
2023-07-03 14:00:35.511  INFO 14848 --- [           main] xx.imports.BootImportsApplication        : Started BootImportsApplication in 2.592 seconds (JVM running for 5.809)
xx.imports.entity.User@3a0807b7

自定义的注解

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

}

注意:刚才新建的UserConfig 要注释掉哦。

修改启动类

在启动类上加上注解 @EnableUser

@EnableUser
@SpringBootApplication
public class BootImportsApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(BootImportsApplication.class, args);
        Object user = context.getBean(User.class);
        System.out.println(user);
    }

}

测试

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.1.RELEASE)

2023-07-03 14:16:54.817  INFO 9640 --- [           main] xx.imports.BootImportsApplication        : Starting BootImportsApplication on ZJZL-20210402GJ with PID 9640 (F:\idea-project\xy-integration-middleware\boot\boot-imports\target\classes started by Administrator in F:\idea-project\xy-integration-middleware)
2023-07-03 14:16:54.828  INFO 9640 --- [           main] xx.imports.BootImportsApplication        : No active profile set, falling back to default profiles: default
2023-07-03 14:16:55.918  INFO 9640 --- [           main] xx.imports.BootImportsApplication        : Started BootImportsApplication in 1.957 seconds (JVM running for 7.231)
xx.imports.entity.User@3174cb09

ImportSelector

如果定义太多注解,那么在main方法上要添加太多注解…因此使用ImportSelector ,ImportSelector 是一个接口,
该接口中只有一个 selectImports 方法,用于返回全类名数组。所以利用该特性我们可以给容器动态导入 N 个 Bean。
因为这两个bean都是通过@Enable类型注解来将配置类注册到IOC容器中,所以我们现在要做的就是将这两个bean一次性都注册到IOC容器中。
在Spring中,要将多个类进行注册,可以使用selector的方式。

新建Role.java

由于需要多个类注入,我们新建一个Role类

public class Role {
}

新建一个ApplicationSelector.java

用来返回多个类名数组

public class ApplicationSelector implements ImportSelector {

    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        return new String[]{
                User.class.getName(),
                Role.class.getName()
        };
    }
}

定义EnableApplicationSelector 注解

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

注意:这里需要将之前定义的 UserConfig、 EnableUser注释掉哦。

修改启动类

@EnableApplicationSelector
@SpringBootApplication
public class BootImportsApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(BootImportsApplication.class, args);
        Object user = context.getBean(User.class);
        System.out.println(user);

        Object role = context.getBean(Role.class);
        System.out.println(role);
    }
}

测试

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.1.RELEASE)

2023-07-03 14:27:08.124  INFO 14616 --- [           main] xx.imports.BootImportsApplication        : Starting BootImportsApplication on ZJZL-20210402GJ with PID 14616 (F:\idea-project\xy-integration-middleware\boot\boot-imports\target\classes started by Administrator in F:\idea-project\xy-integration-middleware)
2023-07-03 14:27:08.135  INFO 14616 --- [           main] xx.imports.BootImportsApplication        : No active profile set, falling back to default profiles: default
2023-07-03 14:27:09.219  INFO 14616 --- [           main] xx.imports.BootImportsApplication        : Started BootImportsApplication in 2.336 seconds (JVM running for 6.605)
xx.imports.entity.User@42a9a63e
xx.imports.entity.Role@62da83ed

ImportBeanDefinitionRegistrar

ImportBeanDefinitionRegistrar 是一个接口,它可以手动注册bean到容器中,从而可以对类进行个性化的定制。

新建 EImportBeanDefinitionRegistrar

实现ImportBeanDefinitionRegistrar接口,并实现其中的方法

public class EImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
        RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(User.class);
        // 注册一个名字叫做 halo 的 bean
        beanDefinitionRegistry.registerBeanDefinition("haloUser", rootBeanDefinition);
    }
}

新建 EImportUserConfig.java

@Import({EImportBeanDefinitionRegistrar.class})
@Configuration
public class EImportUserConfig {
}

修改一下启动类

这里我们通过bean的名称获取

@SpringBootApplication
public class BootImportsApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(BootImportsApplication.class, args);
        Object user = context.getBean("haloUser");
        System.out.println(user);
    }
}

测试

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.1.RELEASE)

2023-07-03 14:32:20.845  INFO 2564 --- [           main] xx.imports.BootImportsApplication        : Starting BootImportsApplication on ZJZL-20210402GJ with PID 2564 (F:\idea-project\xy-integration-middleware\boot\boot-imports\target\classes started by Administrator in F:\idea-project\xy-integration-middleware)
2023-07-03 14:32:20.860  INFO 2564 --- [           main] xx.imports.BootImportsApplication        : No active profile set, falling back to default profiles: default
2023-07-03 14:32:22.476  INFO 2564 --- [           main] xx.imports.BootImportsApplication        : Started BootImportsApplication in 2.949 seconds (JVM running for 7.532)
xx.imports.entity.User@5a4bef8

最后

大家如果觉得写的还不错的话,麻烦大家点个小爱心哦。

<think> 我们被要求使用中文回答,并且遵循特定的格式要求。用户的问题是关于Spring Boot中@EnableScheduling注解使用和相关知识。 根据提供的引用,我们可以总结以下信息: 1. @EnableScheduling注解用于启用Spring的定时任务功能。 2. 使用方法:在Spring配置类(如启动类)上添加该注解即可。 3. 该注解会导入一个配置类SchedulingConfiguration,它是Spring任务调度的核心配置。 4. 添加注解后,就可以编写定时任务方法(通常使用@Scheduled注解标记方法)。 此外,我们还可以补充一些相关知识: - 定时任务方法:需要在方法上使用@Scheduled注解,并配置执行时间(如cron表达式、固定间隔等)。 - 注意事项:默认情况下,定时任务是在同一个线程中执行的,所以如果有多个任务,可能会相互影响(一个任务阻塞会影响后续任务)。因此,对于需要长时间执行的任务,建议配置线程池。 根据以上信息,我们组织回答如下: ### @EnableScheduling注解Spring Boot中的使用 `@EnableScheduling` 是 Spring Framework 提供的一个注解,用于开启对定时任务(Scheduled Tasks)的支持。在 Spring Boot 项目中,我们可以很方便地使用注解来实现定时任务。 #### 1. 使用方法 要使用 `@EnableScheduling` 注解,只需在 Spring Boot 的主配置类(通常是启动类)或任意配置类上添加该注解。例如: ```java @SpringBootApplication @EnableScheduling // 启用定时任务 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` #### 2. 编写定时任务 在启用定时任务后,我们可以在 Spring 管理的 Bean 中编写定时任务方法。定时任务方法需要使用 `@Scheduled` 注解进行标记,并指定执行时间。例如: ```java @Component public class ScheduledTasks { // 每5秒执行一次 @Scheduled(fixedRate = 5000) public void reportCurrentTime() { System.out.println("当前时间:" + new Date()); } // 使用cron表达式,每天12点执行 @Scheduled(cron = "0 0 12 * * ?") public void doSomething() { // 执行任务 } } ``` #### 3. 注解的作用 - `@EnableScheduling` 注解通过 `@Import` 导入 `SchedulingConfiguration` 配置类,该配置类会注册一个 `ScheduledAnnotationBeanPostProcessor` 处理器,用于处理被 `@Scheduled` 注解标记的方法[^2]。 - 该处理器在 Spring 容器初始化时扫描所有 Bean,查找带有 `@Scheduled` 注解的方法,并将它们注册为定时任务。 #### 4. 注意事项 - **单线程问题**:默认情况下,所有的定时任务都在同一个线程(单线程的线程池)中执行。如果某个任务执行时间过长,可能会影响其他任务的执行。为了避免这种情况,可以配置一个自定义的 `TaskScheduler` 线程池。 - **配置线程池**:通过实现 `SchedulingConfigurer` 接口,并重写 `configureTasks` 方法来配置线程池。例如: ```java @Configuration public class SchedulingConfig implements SchedulingConfigurer { @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { taskRegistrar.setScheduler(Executors.newScheduledThreadPool(10)); } } ``` - **避免阻塞**:如果任务执行时间较长,建议让任务异步执行(例如使用 `@Async` 注解配合线程池),或者确保任务内部处理逻辑不会阻塞。 - **集群环境**:在集群部署时,定时任务可能会在多个节点上同时执行。为了避免重复执行,需要使用分布式锁或者将定时任务抽取到单独的节点执行。 #### 5. 总结 `@EnableScheduling` 是启用 Spring 定时任务的关键注解,结合 `@Scheduled` 注解可以轻松实现定时任务调度。但在实际应用中,需要注意线程池配置和集群环境下的任务协调问题。 §§相关问题§§ 1. 如何在Spring Boot中配置定时任务的线程池? 2. `@Scheduled`注解有哪些参数可以配置? 3. 在分布式环境下如何避免定时任务重复执行? 4. 如何动态添加或取消定时任务? [^1]: [@EnableScheduling注解-SpringBoot-Springtask](https://example.com/enable-scheduling-springboot-springtask) [^2]: [Spring Boot@EnableScheduling定时任务注解](https://example.com/spring-boot-enable-scheduling) [^3]: [SpringBoot定时任务(@EnableScheduling注解)](https://example.com/springboot-scheduling-tasks) </think> ### Spring Boot中@EnableScheduling注解详解 `@EnableScheduling` 是 Spring Framework 提供的核心注解,用于启用 Spring 的定时任务调度功能。在 Spring Boot 中集成该功能非常简便,以下是关键知识点: #### 1. **基本作用** - **启用定时任务调度**:激活 Spring 的任务执行器,扫描带有 `@Scheduled` 注解的方法[^2][^3] - **自动配置**:自动注册 `ScheduledAnnotationBeanPostProcessor` 处理器,解析定时任务方法[^2] - **最小化配置**:只需添加该注解即可启用功能,无需额外 XML 配置 #### 2. **使用方法** **步骤 1:在配置类添加注解** ```java @SpringBootApplication @EnableScheduling // 关键注解 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` *注:也可添加到任意 `@Configuration` 配置类[^1][^3]* **步骤 2:编写定时任务** ```java @Component public class TaskService { // 每5秒执行一次 @Scheduled(fixedRate = 5000) public void task1() { System.out.println("定时任务执行: " + new Date()); } // 每天12:00执行(cron表达式) @Scheduled(cron = "0 0 12 * * ?") public void task2() { // 业务逻辑 } } ``` #### 3. **核心实现原理** ```mermaid graph LR A[@EnableScheduling] --> B[导入SchedulingConfiguration配置] B --> C[注册ScheduledAnnotationBeanPostProcessor] C --> D[扫描@Scheduled注解方法] D --> E[创建定时任务调度] ``` - 底层通过 `@Import(SchedulingConfiguration.class)` 导入调度配置[^2] - 依赖 `TaskScheduler` 接口实现任务调度(默认使用 `ThreadPoolTaskScheduler`) #### 4. **注意事项** 1. **线程池配置**: ```yaml spring: task: scheduling: pool: size: 10 # 自定义线程池大小 ``` *默认单线程执行,长时间任务会阻塞后续任务* 2. **集群环境问题**: - 需自行解决分布式环境下的任务幂等性 - 推荐配合分布式锁(如 Redis)或调度中心(如 XXL-JOB) 3. **作用域限制**: - `@Scheduled` 方法必须是无参的 void 方法 - 需声明在 Spring Bean 中(如 `@Component` 类) 4. **执行时间配置**: - `fixedDelay`:上次结束到下次开始的间隔 - `fixedRate`:固定执行频率(不等待上次完成) - `cron`:Unix 风格时间表达式 #### 5. **典型应用场景** - 数据定时同步(如每晚同步用户数据) - 系统状态监控(每分钟检测服务状态) - 缓存刷新(每30分钟刷新热点数据) - 报表生成(每日凌晨生成前日报表)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值