Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
01724d3b
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,发现更多精彩内容 >>
提交
01724d3b
编写于
10月 07, 2014
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Explicitly detect (and prevent) private @Scheduled methods on CGLIB proxies
Issue: SPR-12308
上级
e56559fd
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
52 addition
and
15 deletion
+52
-15
spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.java
...ling/annotation/ScheduledAnnotationBeanPostProcessor.java
+19
-9
spring-context/src/test/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessorTests.java
...annotation/ScheduledAnnotationBeanPostProcessorTests.java
+33
-6
未找到文件。
spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessor.java
浏览文件 @
01724d3b
...
...
@@ -17,6 +17,7 @@
package
org.springframework.scheduling.annotation
;
import
java.lang.reflect.Method
;
import
java.lang.reflect.Modifier
;
import
java.util.Collections
;
import
java.util.HashMap
;
import
java.util.LinkedHashSet
;
...
...
@@ -77,9 +78,8 @@ import org.springframework.util.StringValueResolver;
* @see org.springframework.scheduling.TaskScheduler
* @see org.springframework.scheduling.config.ScheduledTaskRegistrar
*/
public
class
ScheduledAnnotationBeanPostProcessor
implements
BeanPostProcessor
,
Ordered
,
EmbeddedValueResolverAware
,
BeanFactoryAware
,
SmartInitializingSingleton
,
DisposableBean
{
public
class
ScheduledAnnotationBeanPostProcessor
implements
BeanPostProcessor
,
Ordered
,
EmbeddedValueResolverAware
,
BeanFactoryAware
,
SmartInitializingSingleton
,
DisposableBean
{
protected
final
Log
logger
=
LogFactory
.
getLog
(
getClass
());
...
...
@@ -227,17 +227,27 @@ public class ScheduledAnnotationBeanPostProcessor
}
catch
(
NoSuchMethodException
ex
)
{
throw
new
IllegalStateException
(
String
.
format
(
"@Scheduled method '%s' found on bean target class '%s', "
+
"but not found in any interface(s) for bean JDK proxy. Either "
+
"pull the method up to an interface or switch to subclass (CGLIB) "
+
"proxies by setting proxy-target-class/proxyTargetClass "
+
"attribute to 'true'"
,
method
.
getName
(),
method
.
getDeclaringClass
().
getSimpleName
()));
"@Scheduled method '%s' found on bean target class '%s' but not "
+
"found in any interface(s) for a dynamic proxy. Either pull the "
+
"method up to a declared interface or switch to subclass (CGLIB) "
+
"proxies by setting proxy-target-class/proxyTargetClass to 'true'"
,
method
.
getName
(),
method
.
getDeclaringClass
().
getSimpleName
()));
}
}
else
if
(
AopUtils
.
isCglibProxy
(
bean
))
{
// Common problem: private methods end up in the proxy instance, not getting delegated.
if
(
Modifier
.
isPrivate
(
method
.
getModifiers
()))
{
throw
new
IllegalStateException
(
String
.
format
(
"@Scheduled method '%s' found on CGLIB proxy for target class '%s' but cannot "
+
"be delegated to target bean. Switch its visibility to package or protected."
,
method
.
getName
(),
method
.
getDeclaringClass
().
getSimpleName
()));
}
}
Runnable
runnable
=
new
ScheduledMethodRunnable
(
bean
,
method
);
boolean
processedSchedule
=
false
;
String
errorMessage
=
"Exactly one of the 'cron', 'fixedDelay(String)', or 'fixedRate(String)' attributes is required"
;
String
errorMessage
=
"Exactly one of the 'cron', 'fixedDelay(String)', or 'fixedRate(String)' attributes is required"
;
// Determine initial delay
long
initialDelay
=
scheduled
.
initialDelay
();
...
...
spring-context/src/test/java/org/springframework/scheduling/annotation/ScheduledAnnotationBeanPostProcessorTests.java
浏览文件 @
01724d3b
/*
* Copyright 2002-201
3
the original author or authors.
* Copyright 2002-201
4
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.
...
...
@@ -28,6 +28,7 @@ import java.util.List;
import
java.util.Properties
;
import
java.util.TimeZone
;
import
org.junit.After
;
import
org.junit.Test
;
import
org.springframework.beans.DirectFieldAccessor
;
...
...
@@ -46,6 +47,8 @@ import org.springframework.scheduling.support.ScheduledMethodRunnable;
import
org.springframework.scheduling.support.SimpleTriggerContext
;
import
org.springframework.tests.Assume
;
import
org.springframework.tests.TestGroup
;
import
org.springframework.validation.annotation.Validated
;
import
org.springframework.validation.beanvalidation.MethodValidationPostProcessor
;
import
static
org
.
junit
.
Assert
.*;
...
...
@@ -60,6 +63,13 @@ public class ScheduledAnnotationBeanPostProcessorTests {
private
final
StaticApplicationContext
context
=
new
StaticApplicationContext
();
@After
public
void
closeContextAfterTest
()
{
context
.
close
();
}
@Test
public
void
fixedDelayTask
()
{
BeanDefinition
processorDefinition
=
new
RootBeanDefinition
(
ScheduledAnnotationBeanPostProcessor
.
class
);
...
...
@@ -140,7 +150,8 @@ public class ScheduledAnnotationBeanPostProcessorTests {
public
void
severalFixedRatesWithRepeatedScheduledAnnotation
()
{
BeanDefinition
processorDefinition
=
new
RootBeanDefinition
(
ScheduledAnnotationBeanPostProcessor
.
class
);
BeanDefinition
targetDefinition
=
new
RootBeanDefinition
(
SeveralFixedRatesWithRepeatedScheduledAnnotationTestBean
.
class
);
BeanDefinition
targetDefinition
=
new
RootBeanDefinition
(
SeveralFixedRatesWithRepeatedScheduledAnnotationTestBean
.
class
);
severalFixedRates
(
context
,
processorDefinition
,
targetDefinition
);
}
...
...
@@ -155,6 +166,7 @@ public class ScheduledAnnotationBeanPostProcessorTests {
private
void
severalFixedRates
(
StaticApplicationContext
context
,
BeanDefinition
processorDefinition
,
BeanDefinition
targetDefinition
)
{
context
.
registerBeanDefinition
(
"postProcessor"
,
processorDefinition
);
context
.
registerBeanDefinition
(
"target"
,
targetDefinition
);
context
.
refresh
();
...
...
@@ -248,7 +260,8 @@ public class ScheduledAnnotationBeanPostProcessorTests {
Date
lastActualExecutionTime
=
cal
.
getTime
();
cal
.
add
(
Calendar
.
MINUTE
,
30
);
// 4:30
Date
lastCompletionTime
=
cal
.
getTime
();
TriggerContext
triggerContext
=
new
SimpleTriggerContext
(
lastScheduledExecutionTime
,
lastActualExecutionTime
,
lastCompletionTime
);
TriggerContext
triggerContext
=
new
SimpleTriggerContext
(
lastScheduledExecutionTime
,
lastActualExecutionTime
,
lastCompletionTime
);
cal
.
add
(
Calendar
.
MINUTE
,
30
);
cal
.
add
(
Calendar
.
HOUR_OF_DAY
,
1
);
// 6:00
Date
nextExecutionTime
=
cronTrigger
.
nextExecutionTime
(
triggerContext
);
...
...
@@ -269,6 +282,18 @@ public class ScheduledAnnotationBeanPostProcessorTests {
Thread
.
sleep
(
10000
);
}
@Test
(
expected
=
BeanCreationException
.
class
)
public
void
cronTaskWithMethodValidation
()
throws
InterruptedException
{
BeanDefinition
validationDefinition
=
new
RootBeanDefinition
(
MethodValidationPostProcessor
.
class
);
BeanDefinition
processorDefinition
=
new
RootBeanDefinition
(
ScheduledAnnotationBeanPostProcessor
.
class
);
BeanDefinition
targetDefinition
=
new
RootBeanDefinition
(
ScheduledAnnotationBeanPostProcessorTests
.
CronTestBean
.
class
);
context
.
registerBeanDefinition
(
"methodValidation"
,
validationDefinition
);
context
.
registerBeanDefinition
(
"postProcessor"
,
processorDefinition
);
context
.
registerBeanDefinition
(
"target"
,
targetDefinition
);
context
.
refresh
();
}
@Test
public
void
metaAnnotationWithFixedRate
()
{
BeanDefinition
processorDefinition
=
new
RootBeanDefinition
(
ScheduledAnnotationBeanPostProcessor
.
class
);
...
...
@@ -527,10 +552,11 @@ public class ScheduledAnnotationBeanPostProcessorTests {
}
@Validated
static
class
CronTestBean
{
@Scheduled
(
cron
=
"*/7 * * * * ?"
)
p
ublic
void
cron
()
throws
IOException
{
p
rivate
void
cron
()
throws
IOException
{
throw
new
IOException
(
"no no no"
);
}
}
...
...
@@ -539,7 +565,7 @@ public class ScheduledAnnotationBeanPostProcessorTests {
static
class
CronWithTimezoneTestBean
{
@Scheduled
(
cron
=
"0 0 0-4,6-23 * * ?"
,
zone
=
"GMT+10"
)
p
ublic
void
cron
()
throws
IOException
{
p
rotected
void
cron
()
throws
IOException
{
throw
new
IOException
(
"no no no"
);
}
}
...
...
@@ -642,7 +668,8 @@ public class ScheduledAnnotationBeanPostProcessorTests {
@Scheduled
(
cron
=
"${schedules.businessHours}"
)
@Target
(
ElementType
.
METHOD
)
@Retention
(
RetentionPolicy
.
RUNTIME
)
private
static
@interface
BusinessHours
{}
private
static
@interface
BusinessHours
{
}
static
class
PropertyPlaceholderMetaAnnotationTestBean
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录