Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
dk131072
spring-framework
提交
334e4f22
S
spring-framework
项目概览
dk131072
/
spring-framework
与 Fork 源项目一致
从无法访问的项目Fork
通知
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,发现更多精彩内容 >>
提交
334e4f22
编写于
3月 29, 2011
作者:
S
Sam Brannen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[SPR-6184] Initial draft of the new AnnotationConfigContextLoader.
上级
b50338fd
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
273 addition
and
1 deletion
+273
-1
org.springframework.test/src/main/java/org/springframework/test/context/TestContext.java
...in/java/org/springframework/test/context/TestContext.java
+21
-1
org.springframework.test/src/main/java/org/springframework/test/context/support/AnnotationConfigContextLoader.java
...k/test/context/support/AnnotationConfigContextLoader.java
+159
-0
org.springframework.test/src/test/java/org/springframework/test/context/junit4/AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests.java
...4/AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests.java
+31
-0
org.springframework.test/src/test/java/org/springframework/test/context/junit4/AnnotationConfigSpringJUnit4ClassRunnerAppCtxTestsConfig.java
...tationConfigSpringJUnit4ClassRunnerAppCtxTestsConfig.java
+62
-0
未找到文件。
org.springframework.test/src/main/java/org/springframework/test/context/TestContext.java
浏览文件 @
334e4f22
/*
* Copyright 2002-20
09
the original author or authors.
* Copyright 2002-20
11
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.
...
...
@@ -235,6 +235,26 @@ public class TestContext extends AttributeAccessorSupport {
Assert
.
notNull
(
declaringClass
,
"Could not find an 'annotation declaring class' for annotation type ["
+
annotationType
+
"] and class ["
+
clazz
+
"]"
);
// TODO [SPR-6184] Implement recursive search for configuration classes.
// This needs to integrate seamlessly (i.e., analogous yet mutually
// exclusive) with the existing locations search.
ContextConfiguration
cc
=
declaringClass
.
getAnnotation
(
annotationType
);
if
(
cc
!=
null
)
{
if
(
logger
.
isTraceEnabled
())
{
logger
.
trace
(
"Retrieved @ContextConfiguration ["
+
cc
+
"] for declaring class ["
+
declaringClass
+
"]"
);
}
Class
<?>[]
configClasses
=
cc
.
classes
();
if
(!
ObjectUtils
.
isEmpty
(
configClasses
))
{
for
(
Class
<?>
configClass
:
configClasses
)
{
locationsList
.
add
(
configClass
.
getName
());
}
return
locationsList
.
toArray
(
new
String
[
locationsList
.
size
()]);
}
}
while
(
declaringClass
!=
null
)
{
ContextConfiguration
contextConfiguration
=
declaringClass
.
getAnnotation
(
annotationType
);
if
(
logger
.
isTraceEnabled
())
{
...
...
org.springframework.test/src/main/java/org/springframework/test/context/support/AnnotationConfigContextLoader.java
0 → 100644
浏览文件 @
334e4f22
/*
* Copyright 2002-2011 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.test.context.support
;
import
java.util.ArrayList
;
import
java.util.List
;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.LogFactory
;
import
org.springframework.beans.factory.support.DefaultListableBeanFactory
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.context.annotation.AnnotationConfigUtils
;
import
org.springframework.context.support.GenericApplicationContext
;
import
org.springframework.util.ObjectUtils
;
/**
* TODO Document AnnotationConfigContextLoader.
*
* @author Sam Brannen
* @since 3.1
*/
public
class
AnnotationConfigContextLoader
extends
AbstractContextLoader
{
private
static
final
Log
logger
=
LogFactory
.
getLog
(
AnnotationConfigContextLoader
.
class
);
/**
* TODO Document loadContext().
*
* @see org.springframework.test.context.ContextLoader#loadContext(java.lang.String[])
*/
public
ApplicationContext
loadContext
(
String
...
locations
)
throws
Exception
{
if
(
logger
.
isDebugEnabled
())
{
logger
.
debug
(
"Creating an AnnotationConfigApplicationContext for "
+
ObjectUtils
.
nullSafeToString
(
locations
));
}
AnnotationConfigApplicationContext
context
=
new
AnnotationConfigApplicationContext
();
prepareContext
(
context
);
customizeBeanFactory
(
context
.
getDefaultListableBeanFactory
());
List
<
Class
<?>>
configClasses
=
new
ArrayList
<
Class
<?>>();
for
(
String
location
:
locations
)
{
final
Class
<?>
clazz
=
getClass
().
getClassLoader
().
loadClass
(
location
);
configClasses
.
add
(
clazz
);
}
if
(
logger
.
isDebugEnabled
())
{
logger
.
debug
(
"Loading AnnotationConfigApplicationContext from config classes: "
+
configClasses
);
}
for
(
Class
<?>
configClass
:
configClasses
)
{
context
.
register
(
configClass
);
}
AnnotationConfigUtils
.
registerAnnotationConfigProcessors
(
context
);
customizeContext
(
context
);
context
.
refresh
();
context
.
registerShutdownHook
();
return
context
;
}
/**
* Prepare the {@link GenericApplicationContext} created by this
* ContextLoader. Called <i>before</> bean definitions are read.
* <p>
* The default implementation is empty. Can be overridden in subclasses to
* customize GenericApplicationContext's standard settings.
*
* @param context the context for which the BeanDefinitionReader should be
* created
* @see #loadContext
* @see org.springframework.context.support.GenericApplicationContext#setResourceLoader
* @see org.springframework.context.support.GenericApplicationContext#setId
*/
protected
void
prepareContext
(
GenericApplicationContext
context
)
{
}
/**
* Customize the internal bean factory of the ApplicationContext created by
* this ContextLoader.
* <p>
* The default implementation is empty but can be overridden in subclasses
* to customize DefaultListableBeanFactory's standard settings.
*
* @param beanFactory the bean factory created by this ContextLoader
* @see #loadContext
* @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowBeanDefinitionOverriding(boolean)
* @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowEagerClassLoading(boolean)
* @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowCircularReferences(boolean)
* @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowRawInjectionDespiteWrapping(boolean)
*/
protected
void
customizeBeanFactory
(
DefaultListableBeanFactory
beanFactory
)
{
}
/**
* Customize the {@link GenericApplicationContext} created by this
* ContextLoader <i>after</i> bean definitions have been loaded into the
* context but before the context is refreshed.
* <p>
* The default implementation is empty but can be overridden in subclasses
* to customize the application context.
*
* @param context the newly created application context
* @see #loadContext(String...)
*/
protected
void
customizeContext
(
GenericApplicationContext
context
)
{
}
/**
* TODO Document overridden generateDefaultLocations().
*
* @see org.springframework.test.context.support.AbstractContextLoader#generateDefaultLocations(java.lang.Class)
*/
@Override
protected
String
[]
generateDefaultLocations
(
Class
<?>
clazz
)
{
// TODO Implement generateDefaultLocations().
throw
new
UnsupportedOperationException
(
"Not yet implemented"
);
}
/**
* TODO Document modifyLocations().
*
* @see org.springframework.test.context.support.AbstractContextLoader#modifyLocations(java.lang.Class,
* java.lang.String[])
*/
@Override
protected
String
[]
modifyLocations
(
Class
<?>
clazz
,
String
...
locations
)
{
// TODO Implement modifyLocations() (?).
return
locations
;
}
/**
* TODO Document getResourceSuffix().
*
* @see org.springframework.test.context.support.AbstractContextLoader#getResourceSuffix()
*/
@Override
protected
String
getResourceSuffix
()
{
return
"Config"
;
}
}
org.springframework.test/src/test/java/org/springframework/test/context/junit4/AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests.java
0 → 100644
浏览文件 @
334e4f22
/*
* Copyright 2002-2011 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.test.context.junit4
;
import
org.springframework.test.context.ContextConfiguration
;
import
org.springframework.test.context.support.AnnotationConfigContextLoader
;
/**
* TODO [SPR-6184] Document AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests.
*
* @author Sam Brannen
* @since 3.1
*/
@ContextConfiguration
(
loader
=
AnnotationConfigContextLoader
.
class
,
classes
=
AnnotationConfigSpringJUnit4ClassRunnerAppCtxTestsConfig
.
class
,
inheritLocations
=
false
)
public
class
AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests
extends
SpringJUnit4ClassRunnerAppCtxTests
{
/* all tests are in the parent class. */
}
org.springframework.test/src/test/java/org/springframework/test/context/junit4/AnnotationConfigSpringJUnit4ClassRunnerAppCtxTestsConfig.java
0 → 100644
浏览文件 @
334e4f22
/*
* Copyright 2002-2011 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.test.context.junit4
;
import
org.springframework.beans.Employee
;
import
org.springframework.beans.Pet
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
/**
* TODO [SPR-6184] Document configuration class.
*
* @author Sam Brannen
* @since 3.1
*/
@Configuration
public
class
AnnotationConfigSpringJUnit4ClassRunnerAppCtxTestsConfig
{
@Bean
public
Employee
employee
()
{
Employee
employee
=
new
Employee
();
employee
.
setName
(
"John Smith"
);
employee
.
setAge
(
42
);
employee
.
setCompany
(
"Acme Widgets, Inc."
);
return
employee
;
}
@Bean
public
Pet
pet
()
{
return
new
Pet
(
"Fido"
);
}
@Bean
public
String
foo
()
{
return
"Foo"
;
}
@Bean
public
String
bar
()
{
return
"Bar"
;
}
@Bean
public
String
quux
()
{
return
"Quux"
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录