背景
最近開始做個(gè)小項(xiàng)目,用到了Spring Boot,在看前輩書寫的代碼時(shí),發(fā)現(xiàn)其中有許多注解是我之前從來沒接觸過的。于是決定每天抽點(diǎn)時(shí)間,學(xué)習(xí)總結(jié)一下這些陌生的注解,讓以后的工作更加有效率。
在Spring Boot 的入口類 XXXApplication 中,必然會(huì)有@SpringBootApplication,用來標(biāo)注項(xiàng)目入口,以及完成一些基本的自動(dòng)自動(dòng)配置。但是,在這次項(xiàng)目中,XXXApplication 類中除了@SpringBootApplication注解,還用到了另一個(gè)注解,也就是下面要學(xué)習(xí)的@EnableScheduling注解。
分析
在沒有開始學(xué)習(xí)該注解相關(guān)的源碼之前,可以從其名稱先分析一下作用。
Enable,能夠、使...可以。
Schedule, 任務(wù)計(jì)劃、日程。ing形式也就是取其動(dòng)詞形式的語義。
那么也就是說這個(gè)注解是用來使計(jì)劃任務(wù)功能可以使用的注解。
那么,新的問題就來了,對(duì)于沒有接觸過 Spring 中“計(jì)劃任務(wù)”的我而言,計(jì)劃任務(wù)又是什么呢?
下來,就可以帶著問題去看看這個(gè)注解的定義類了。
問題:
- 這個(gè)注解的功能是不是如分析的一樣,是啟動(dòng)某種功能的注解?
- 關(guān)于 “計(jì)劃任務(wù)” 的定義是什么?
話不多說,直接上定義類源碼:
/*
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.scheduling.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 java.util.concurrent.Executor;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
/**
* Enables Spring's scheduled task execution capability, similar to
* functionality found in Spring's {@code <task:*>} XML namespace. To be used
* on @{@link Configuration} classes as follows:
*
* <pre class="code">
* @Configuration
* @EnableScheduling
* public class AppConfig {
*
* // various @Bean definitions
* }</pre>
*
* This enables detection of @{@link Scheduled} annotations on any Spring-managed
* bean in the container. For example, given a class {@code MyTask}
*
* <pre class="code">
* package com.myco.tasks;
*
* public class MyTask {
*
* @Scheduled(fixedRate=1000)
* public void work() {
* // task execution logic
* }
* }</pre>
*
* the following configuration would ensure that {@code MyTask.work()} is called
* once every 1000 ms:
*
* <pre class="code">
* @Configuration
* @EnableScheduling
* public class AppConfig {
*
* @Bean
* public MyTask task() {
* return new MyTask();
* }
* }</pre>
*
* Alternatively, if {@code MyTask} were annotated with {@code @Component}, the
* following configuration would ensure that its {@code @Scheduled} method is
* invoked at the desired interval:
*
* <pre class="code">
* @Configuration
* @EnableScheduling
* @ComponentScan(basePackages="com.myco.tasks")
* public class AppConfig {
* }</pre>
*
* Methods annotated with {@code @Scheduled} may even be declared directly within
* {@code @Configuration} classes:
*
* <pre class="code">
* @Configuration
* @EnableScheduling
* public class AppConfig {
*
* @Scheduled(fixedRate=1000)
* public void work() {
* // task execution logic
* }
* }</pre>
*
* <p>By default, will be searching for an associated scheduler definition: either
* a unique {@link org.springframework.scheduling.TaskScheduler} bean in the context,
* or a {@code TaskScheduler} bean named "taskScheduler" otherwise; the same lookup
* will also be performed for a {@link java.util.concurrent.ScheduledExecutorService}
* bean. If neither of the two is resolvable, a local single-threaded default
* scheduler will be created and used within the registrar.
*
* <p>When more control is desired, a {@code @Configuration} class may implement
* {@link SchedulingConfigurer}. This allows access to the underlying
* {@link ScheduledTaskRegistrar} instance. For example, the following example
* demonstrates how to customize the {@link Executor} used to execute scheduled
* tasks:
*
* <pre class="code">
* @Configuration
* @EnableScheduling
* public class AppConfig implements SchedulingConfigurer {
*
* @Override
* public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
* taskRegistrar.setScheduler(taskExecutor());
* }
*
* @Bean(destroyMethod="shutdown")
* public Executor taskExecutor() {
* return Executors.newScheduledThreadPool(100);
* }
* }</pre>
*
* <p>Note in the example above the use of {@code @Bean(destroyMethod="shutdown")}.
* This ensures that the task executor is properly shut down when the Spring
* application context itself is closed.
*
* <p>Implementing {@code SchedulingConfigurer} also allows for fine-grained
* control over task registration via the {@code ScheduledTaskRegistrar}.
* For example, the following configures the execution of a particular bean
* method per a custom {@code Trigger} implementation:
*
* <pre class="code">
* @Configuration
* @EnableScheduling
* public class AppConfig implements SchedulingConfigurer {
*
* @Override
* public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
* taskRegistrar.setScheduler(taskScheduler());
* taskRegistrar.addTriggerTask(
* new Runnable() {
* public void run() {
* myTask().work();
* }
* },
* new CustomTrigger()
* );
* }
*
* @Bean(destroyMethod="shutdown")
* public Executor taskScheduler() {
* return Executors.newScheduledThreadPool(42);
* }
*
* @Bean
* public MyTask myTask() {
* return new MyTask();
* }
* }</pre>
*
* <p>For reference, the example above can be compared to the following Spring XML
* configuration:
*
* <pre class="code">
* {@code
* <beans>
*
* <task:annotation-driven scheduler="taskScheduler"/>
*
* <task:scheduler id="taskScheduler" pool-size="42"/>
*
* <task:scheduled-tasks scheduler="taskScheduler">
* <task:scheduled ref="myTask" method="work" fixed-rate="1000"/>
* </task:scheduled-tasks>
*
* <bean id="myTask" class="com.foo.MyTask"/>
*
* </beans>
* }</pre>
*
* The examples are equivalent save that in XML a <em>fixed-rate</em> period is used
* instead of a custom <em>{@code Trigger}</em> implementation; this is because the
* {@code task:} namespace {@code scheduled} cannot easily expose such support. This is
* but one demonstration how the code-based approach allows for maximum configurability
* through direct access to actual componentry.<p>
*
* @author Chris Beams
* @author Juergen Hoeller
* @since 3.1
* @see Scheduled
* @see SchedulingConfiguration
* @see SchedulingConfigurer
* @see ScheduledTaskRegistrar
* @see Trigger
* @see ScheduledAnnotationBeanPostProcessor
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(SchedulingConfiguration.class)
@Documented
public @interface EnableScheduling {
}
從注釋的第一句話就可以解答第一個(gè)問題,這個(gè)注解確實(shí)是啟動(dòng)前面所說的“計(jì)劃任務(wù)”功能的,類似于用XML配置 Spring 時(shí)的 task 標(biāo)簽的作用。
可是這個(gè)執(zhí)行“計(jì)劃任務(wù)”的功能又是什么呢?
范例下面有解釋:
當(dāng)開發(fā)者在AppConfig類中使用了本注解,并在某個(gè)Task類中使用了@Schedule注解,那么被@Schedule注解的標(biāo)注的方法就可以在指定時(shí)間自動(dòng)執(zhí)行。
那么這個(gè)“計(jì)劃任務(wù)”其實(shí)被稱為“定時(shí)任務(wù)”更為合適。
總結(jié)
關(guān)于@EnableScheduling,其實(shí)就是用來使@Schedule注解功能可用的注解。在 Spring Boot 的配置類中,標(biāo)注上這個(gè)注解,就可以對(duì)項(xiàng)目中的方法某些方法使用@Schedule注解,將其變?yōu)槎〞r(shí)自動(dòng)執(zhí)行。
但只有兩種注解共同使用時(shí),才能達(dá)到本注解應(yīng)有的作用。