Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
410dd0aa
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
410dd0aa
编写于
1月 11, 2010
作者:
M
Mark Fisher
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
SPR-6670 @Scheduled now supports property placeholders for cron expressions.
上级
1284086f
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
90 addition
and
1 deletion
+90
-1
org.springframework.context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.java
...ling/annotation/ScheduledAnnotationBeanPostProcessor.java
+6
-1
org.springframework.context/src/test/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessorTests.java
...annotation/ScheduledAnnotationBeanPostProcessorTests.java
+84
-0
未找到文件。
org.springframework.context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.java
浏览文件 @
410dd0aa
/*
* Copyright 2002-20
09
the original author or authors.
* Copyright 2002-20
10
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.
...
...
@@ -27,6 +27,7 @@ import org.springframework.beans.factory.config.BeanPostProcessor;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.ApplicationContextAware
;
import
org.springframework.context.ApplicationListener
;
import
org.springframework.context.ConfigurableApplicationContext
;
import
org.springframework.context.event.ContextRefreshedEvent
;
import
org.springframework.core.Ordered
;
import
org.springframework.core.annotation.AnnotationUtils
;
...
...
@@ -113,6 +114,10 @@ public class ScheduledAnnotationBeanPostProcessor implements BeanPostProcessor,
String
cron
=
annotation
.
cron
();
if
(!
""
.
equals
(
cron
))
{
processedSchedule
=
true
;
if
(
applicationContext
instanceof
ConfigurableApplicationContext
)
{
cron
=
((
ConfigurableApplicationContext
)
applicationContext
)
.
getBeanFactory
().
resolveEmbeddedValue
(
cron
);
}
cronTasks
.
put
(
runnable
,
cron
);
}
long
fixedDelay
=
annotation
.
fixedDelay
();
...
...
org.springframework.context/src/test/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessorTests.java
浏览文件 @
410dd0aa
...
...
@@ -23,12 +23,14 @@ import java.lang.annotation.Retention;
import
java.lang.annotation.RetentionPolicy
;
import
java.lang.annotation.Target
;
import
java.util.Map
;
import
java.util.Properties
;
import
org.junit.Test
;
import
org.springframework.beans.DirectFieldAccessor
;
import
org.springframework.beans.factory.BeanCreationException
;
import
org.springframework.beans.factory.config.BeanDefinition
;
import
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
;
import
org.springframework.beans.factory.support.RootBeanDefinition
;
import
org.springframework.context.support.StaticApplicationContext
;
import
org.springframework.scheduling.config.ScheduledTaskRegistrar
;
...
...
@@ -160,6 +162,66 @@ public class ScheduledAnnotationBeanPostProcessorTests {
assertEquals
(
"0 0 * * * ?"
,
cronTasks
.
values
().
iterator
().
next
());
}
@Test
public
void
propertyPlaceholderWithCronExpression
()
{
String
businessHoursCronExpression
=
"0 0 9-17 * * MON-FRI"
;
StaticApplicationContext
context
=
new
StaticApplicationContext
();
BeanDefinition
processorDefinition
=
new
RootBeanDefinition
(
ScheduledAnnotationBeanPostProcessor
.
class
);
BeanDefinition
placeholderDefinition
=
new
RootBeanDefinition
(
PropertyPlaceholderConfigurer
.
class
);
Properties
properties
=
new
Properties
();
properties
.
setProperty
(
"schedules.businessHours"
,
businessHoursCronExpression
);
placeholderDefinition
.
getPropertyValues
().
addPropertyValue
(
"properties"
,
properties
);
BeanDefinition
targetDefinition
=
new
RootBeanDefinition
(
ScheduledAnnotationBeanPostProcessorTests
.
PropertyPlaceholderTestBean
.
class
);
context
.
registerBeanDefinition
(
"placeholder"
,
placeholderDefinition
);
context
.
registerBeanDefinition
(
"postProcessor"
,
processorDefinition
);
context
.
registerBeanDefinition
(
"target"
,
targetDefinition
);
context
.
refresh
();
Object
postProcessor
=
context
.
getBean
(
"postProcessor"
);
Object
target
=
context
.
getBean
(
"target"
);
ScheduledTaskRegistrar
registrar
=
(
ScheduledTaskRegistrar
)
new
DirectFieldAccessor
(
postProcessor
).
getPropertyValue
(
"registrar"
);
Map
<
Runnable
,
String
>
cronTasks
=
(
Map
<
Runnable
,
String
>)
new
DirectFieldAccessor
(
registrar
).
getPropertyValue
(
"cronTasks"
);
assertEquals
(
1
,
cronTasks
.
size
());
MethodInvokingRunnable
runnable
=
(
MethodInvokingRunnable
)
cronTasks
.
keySet
().
iterator
().
next
();
Object
targetObject
=
runnable
.
getTargetObject
();
String
targetMethod
=
runnable
.
getTargetMethod
();
assertEquals
(
target
,
targetObject
);
assertEquals
(
"x"
,
targetMethod
);
assertEquals
(
businessHoursCronExpression
,
cronTasks
.
values
().
iterator
().
next
());
}
@Test
public
void
propertyPlaceholderForMetaAnnotation
()
{
String
businessHoursCronExpression
=
"0 0 9-17 * * MON-FRI"
;
StaticApplicationContext
context
=
new
StaticApplicationContext
();
BeanDefinition
processorDefinition
=
new
RootBeanDefinition
(
ScheduledAnnotationBeanPostProcessor
.
class
);
BeanDefinition
placeholderDefinition
=
new
RootBeanDefinition
(
PropertyPlaceholderConfigurer
.
class
);
Properties
properties
=
new
Properties
();
properties
.
setProperty
(
"schedules.businessHours"
,
businessHoursCronExpression
);
placeholderDefinition
.
getPropertyValues
().
addPropertyValue
(
"properties"
,
properties
);
BeanDefinition
targetDefinition
=
new
RootBeanDefinition
(
ScheduledAnnotationBeanPostProcessorTests
.
PropertyPlaceholderMetaAnnotationTestBean
.
class
);
context
.
registerBeanDefinition
(
"postProcessor"
,
processorDefinition
);
context
.
registerBeanDefinition
(
"placeholder"
,
placeholderDefinition
);
context
.
registerBeanDefinition
(
"target"
,
targetDefinition
);
context
.
refresh
();
Object
postProcessor
=
context
.
getBean
(
"postProcessor"
);
Object
target
=
context
.
getBean
(
"target"
);
ScheduledTaskRegistrar
registrar
=
(
ScheduledTaskRegistrar
)
new
DirectFieldAccessor
(
postProcessor
).
getPropertyValue
(
"registrar"
);
Map
<
Runnable
,
String
>
cronTasks
=
(
Map
<
Runnable
,
String
>)
new
DirectFieldAccessor
(
registrar
).
getPropertyValue
(
"cronTasks"
);
assertEquals
(
1
,
cronTasks
.
size
());
MethodInvokingRunnable
runnable
=
(
MethodInvokingRunnable
)
cronTasks
.
keySet
().
iterator
().
next
();
Object
targetObject
=
runnable
.
getTargetObject
();
String
targetMethod
=
runnable
.
getTargetMethod
();
assertEquals
(
target
,
targetObject
);
assertEquals
(
"y"
,
targetMethod
);
assertEquals
(
businessHoursCronExpression
,
cronTasks
.
values
().
iterator
().
next
());
}
@Test
(
expected
=
BeanCreationException
.
class
)
public
void
emptyAnnotation
()
{
StaticApplicationContext
context
=
new
StaticApplicationContext
();
...
...
@@ -296,4 +358,26 @@ public class ScheduledAnnotationBeanPostProcessorTests {
}
}
private
static
class
PropertyPlaceholderTestBean
{
@Scheduled
(
cron
=
"${schedules.businessHours}"
)
public
void
x
()
{
}
}
@Scheduled
(
cron
=
"${schedules.businessHours}"
)
@Target
(
ElementType
.
METHOD
)
@Retention
(
RetentionPolicy
.
RUNTIME
)
private
static
@interface
BusinessHours
{}
private
static
class
PropertyPlaceholderMetaAnnotationTestBean
{
@BusinessHours
public
void
y
()
{
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录