public class ScheduledAnnotationBeanPostProcessor extends Object implements BeanPostProcessor, Ordered, EmbeddedValueResolverAware, BeanFactoryAware, ApplicationContextAware, SmartInitializingSingleton, ApplicationListener<ContextRefreshedEvent>, DisposableBean
Scheduled
to be invoked by a TaskScheduler
according
to the "fixedRate", "fixedDelay", or "cron" expression provided via the annotation.
This post-processor is automatically registered by Spring's
<task:annotation-driven>
XML element, and also by the
@EnableScheduling
annotation.
Autodetects any SchedulingConfigurer
instances in the container,
allowing for customization of the scheduler to be used or for fine-grained
control over task registration (e.g. registration of Trigger
tasks.
See the @EnableScheduling
javadocs for complete usage details.
Scheduled
,
EnableScheduling
,
SchedulingConfigurer
,
TaskScheduler
,
ScheduledTaskRegistrar
,
AsyncAnnotationBeanPostProcessor
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_TASK_SCHEDULER_BEAN_NAME
The default name of the
TaskScheduler bean to pick up: "taskScheduler". |
protected Log |
logger |
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
Constructor and Description |
---|
ScheduledAnnotationBeanPostProcessor() |
Modifier and Type | Method and Description |
---|---|
void |
afterSingletonsInstantiated()
Invoked right at the end of the singleton pre-instantiation phase,
with a guarantee that all regular singleton beans have been created
already.
|
void |
destroy()
Invoked by a BeanFactory on destruction of a singleton.
|
int |
getOrder()
Get the order value of this object.
|
void |
onApplicationEvent(ContextRefreshedEvent event)
Handle an application event.
|
Object |
postProcessAfterInitialization(Object bean,
String beanName)
Apply this BeanPostProcessor to the given new bean instance after any bean
initialization callbacks (like InitializingBean's
afterPropertiesSet
or a custom init-method). |
Object |
postProcessBeforeInitialization(Object bean,
String beanName)
Apply this BeanPostProcessor to the given new bean instance before any bean
initialization callbacks (like InitializingBean's
afterPropertiesSet
or a custom init-method). |
protected void |
processScheduled(Scheduled scheduled,
Method method,
Object bean) |
void |
setApplicationContext(ApplicationContext applicationContext)
Setting an
ApplicationContext is optional: If set, registered
tasks will be activated in the ContextRefreshedEvent phase;
if not set, it will happen at afterSingletonsInstantiated() time. |
void |
setBeanFactory(BeanFactory beanFactory)
Making a
BeanFactory available is optional; if not set,
SchedulingConfigurer beans won't get autodetected and
a scheduler has to be explicitly configured. |
void |
setEmbeddedValueResolver(StringValueResolver resolver)
Set the StringValueResolver to use for resolving embedded definition values.
|
void |
setScheduler(Object scheduler)
Set the
TaskScheduler that will invoke
the scheduled methods, or a ScheduledExecutorService
to be wrapped as a TaskScheduler. |
public static final String DEFAULT_TASK_SCHEDULER_BEAN_NAME
TaskScheduler
bean to pick up: "taskScheduler".
Note that the initial lookup happens by type; this is just the fallback in case of multiple scheduler beans found in the context.
protected final Log logger
public ScheduledAnnotationBeanPostProcessor()
public int getOrder()
Ordered
Higher values are interpreted as lower priority. As a consequence,
the object with the lowest value has the highest priority (somewhat
analogous to Servlet load-on-startup
values).
Same order values will result in arbitrary sort positions for the affected objects.
getOrder
in interface Ordered
Ordered.HIGHEST_PRECEDENCE
,
Ordered.LOWEST_PRECEDENCE
public void setScheduler(Object scheduler)
TaskScheduler
that will invoke
the scheduled methods, or a ScheduledExecutorService
to be wrapped as a TaskScheduler.public void setEmbeddedValueResolver(StringValueResolver resolver)
EmbeddedValueResolverAware
setEmbeddedValueResolver
in interface EmbeddedValueResolverAware
public void setBeanFactory(BeanFactory beanFactory)
BeanFactory
available is optional; if not set,
SchedulingConfigurer
beans won't get autodetected and
a scheduler
has to be explicitly configured.setBeanFactory
in interface BeanFactoryAware
beanFactory
- owning BeanFactory (never null
).
The bean can immediately call methods on the factory.BeanInitializationException
public void setApplicationContext(ApplicationContext applicationContext)
ApplicationContext
is optional: If set, registered
tasks will be activated in the ContextRefreshedEvent
phase;
if not set, it will happen at afterSingletonsInstantiated()
time.setApplicationContext
in interface ApplicationContextAware
applicationContext
- the ApplicationContext object to be used by this objectBeanInitializationException
public void afterSingletonsInstantiated()
SmartInitializingSingleton
ListableBeanFactory.getBeansOfType(java.lang.Class<T>)
calls within
this method won't trigger accidental side effects during bootstrap.
NOTE: This callback won't be triggered for singleton beans
lazily initialized on demand after BeanFactory
bootstrap,
and not for any other bean scope either. Carefully use it for beans
with the intended bootstrap semantics only.
afterSingletonsInstantiated
in interface SmartInitializingSingleton
public void onApplicationEvent(ContextRefreshedEvent event)
ApplicationListener
onApplicationEvent
in interface ApplicationListener<ContextRefreshedEvent>
event
- the event to respond topublic Object postProcessBeforeInitialization(Object bean, String beanName)
BeanPostProcessor
afterPropertiesSet
or a custom init-method). The bean will already be populated with property values.
The returned bean instance may be a wrapper around the original.postProcessBeforeInitialization
in interface BeanPostProcessor
bean
- the new bean instancebeanName
- the name of the beannull
, no subsequent BeanPostProcessors will be invokedInitializingBean.afterPropertiesSet()
public Object postProcessAfterInitialization(Object bean, String beanName)
BeanPostProcessor
afterPropertiesSet
or a custom init-method). The bean will already be populated with property values.
The returned bean instance may be a wrapper around the original.
In case of a FactoryBean, this callback will be invoked for both the FactoryBean
instance and the objects created by the FactoryBean (as of Spring 2.0). The
post-processor can decide whether to apply to either the FactoryBean or created
objects or both through corresponding bean instanceof FactoryBean
checks.
This callback will also be invoked after a short-circuiting triggered by a
InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation(java.lang.Class<?>, java.lang.String)
method,
in contrast to all other BeanPostProcessor callbacks.
postProcessAfterInitialization
in interface BeanPostProcessor
bean
- the new bean instancebeanName
- the name of the beannull
, no subsequent BeanPostProcessors will be invokedInitializingBean.afterPropertiesSet()
,
FactoryBean
public void destroy()
DisposableBean
destroy
in interface DisposableBean