From fd265a18c619b21f97f753871c6e1c32df65ba32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Mon, 4 Jul 2022 14:18:27 +0200 Subject: [PATCH] Add runtime hints for scheduling support Closes gh-28696 --- .../scheduling/annotation/Scheduled.java | 2 + .../ScheduledAnnotationBeanPostProcessor.java | 2 + ...eduledAnnotationRuntimeHintsRegistrar.java | 37 +++++++++++++++++++ .../scheduling/annotation/Schedules.java | 3 ++ 4 files changed, 44 insertions(+) create mode 100644 spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationRuntimeHintsRegistrar.java diff --git a/spring-context/src/main/java/org/springframework/scheduling/annotation/Scheduled.java b/spring-context/src/main/java/org/springframework/scheduling/annotation/Scheduled.java index e3f4de5408..1b4cca0778 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/annotation/Scheduled.java +++ b/spring-context/src/main/java/org/springframework/scheduling/annotation/Scheduled.java @@ -24,6 +24,7 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.util.concurrent.TimeUnit; +import org.springframework.aot.hint.annotation.Reflective; import org.springframework.scheduling.config.ScheduledTaskRegistrar; /** @@ -61,6 +62,7 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar; @Retention(RetentionPolicy.RUNTIME) @Documented @Repeatable(Schedules.class) +@Reflective public @interface Scheduled { /** diff --git a/spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.java b/spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.java index cf833983c3..23eb0de53b 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.java +++ b/spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.java @@ -55,6 +55,7 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationListener; import org.springframework.context.EmbeddedValueResolverAware; +import org.springframework.context.annotation.ImportRuntimeHints; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.core.MethodIntrospector; import org.springframework.core.Ordered; @@ -106,6 +107,7 @@ import org.springframework.util.StringValueResolver; * @see org.springframework.scheduling.config.ScheduledTaskRegistrar * @see AsyncAnnotationBeanPostProcessor */ +@ImportRuntimeHints(ScheduledAnnotationRuntimeHintsRegistrar.class) public class ScheduledAnnotationBeanPostProcessor implements ScheduledTaskHolder, MergedBeanDefinitionPostProcessor, DestructionAwareBeanPostProcessor, Ordered, EmbeddedValueResolverAware, BeanNameAware, BeanFactoryAware, ApplicationContextAware, diff --git a/spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationRuntimeHintsRegistrar.java b/spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationRuntimeHintsRegistrar.java new file mode 100644 index 0000000000..c92f9b9042 --- /dev/null +++ b/spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationRuntimeHintsRegistrar.java @@ -0,0 +1,37 @@ +/* + * Copyright 2002-2022 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 + * + * https://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 org.springframework.aot.hint.RuntimeHints; +import org.springframework.aot.hint.RuntimeHintsRegistrar; +import org.springframework.aot.hint.support.RuntimeHintsUtils; + +/** + * {@link RuntimeHintsRegistrar} implementation that registers runtime hints for + * {@link Scheduled} annotation. + * + * @author Sebastien Deleuze + * @since 6.0 + */ +public class ScheduledAnnotationRuntimeHintsRegistrar implements RuntimeHintsRegistrar { + + @Override + public void registerHints(RuntimeHints hints, ClassLoader classLoader) { + RuntimeHintsUtils.registerAnnotation(hints, Scheduled.class); + RuntimeHintsUtils.registerAnnotation(hints, Schedules.class); + } +} diff --git a/spring-context/src/main/java/org/springframework/scheduling/annotation/Schedules.java b/spring-context/src/main/java/org/springframework/scheduling/annotation/Schedules.java index fa8e970347..da5f026d9d 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/annotation/Schedules.java +++ b/spring-context/src/main/java/org/springframework/scheduling/annotation/Schedules.java @@ -22,6 +22,8 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import org.springframework.aot.hint.annotation.Reflective; + /** * Container annotation that aggregates several {@link Scheduled} annotations. * @@ -40,6 +42,7 @@ import java.lang.annotation.Target; @Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented +@Reflective public @interface Schedules { Scheduled[] value();