Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
f2e4ad23
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 搜索 >>
提交
f2e4ad23
编写于
8月 08, 2014
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
ComponentScan annotation allows for registering beans with lazy initialization
Issue: SPR-10459
上级
ef51d4db
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
57 addition
and
29 deletion
+57
-29
spring-context/src/main/java/org/springframework/context/annotation/ClassPathBeanDefinitionScanner.java
...rk/context/annotation/ClassPathBeanDefinitionScanner.java
+9
-1
spring-context/src/main/java/org/springframework/context/annotation/ComponentScan.java
...org/springframework/context/annotation/ComponentScan.java
+8
-1
spring-context/src/main/java/org/springframework/context/annotation/ComponentScanAnnotationParser.java
...ork/context/annotation/ComponentScanAnnotationParser.java
+6
-1
spring-context/src/test/java/org/springframework/context/annotation/ComponentScanAnnotationIntegrationTests.java
...t/annotation/ComponentScanAnnotationIntegrationTests.java
+18
-16
spring-context/src/test/java/org/springframework/context/annotation/ComponentScanAnnotationTests.java
...work/context/annotation/ComponentScanAnnotationTests.java
+11
-6
spring-context/src/test/java/org/springframework/context/annotation/ComponentScanParserTests.java
...ramework/context/annotation/ComponentScanParserTests.java
+5
-4
未找到文件。
spring-context/src/main/java/org/springframework/context/annotation/ClassPathBeanDefinitionScanner.java
浏览文件 @
f2e4ad23
/*
* 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.
...
...
@@ -163,6 +163,14 @@ public class ClassPathBeanDefinitionScanner extends ClassPathScanningCandidateCo
(
beanDefinitionDefaults
!=
null
?
beanDefinitionDefaults
:
new
BeanDefinitionDefaults
());
}
/**
* Return the defaults to use for detected beans (never {@code null}).
* @since 4.1
*/
public
BeanDefinitionDefaults
getBeanDefinitionDefaults
()
{
return
this
.
beanDefinitionDefaults
;
}
/**
* Set the name-matching patterns for determining autowire candidates.
* @param autowireCandidatePatterns the patterns to match against
...
...
spring-context/src/main/java/org/springframework/context/annotation/ComponentScan.java
浏览文件 @
f2e4ad23
/*
* 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.
...
...
@@ -132,6 +132,13 @@ public @interface ComponentScan {
*/
Filter
[]
excludeFilters
()
default
{};
/**
* Specify whether scanned beans should be registered for lazy initialization.
* <p>Default is {@code false}; switch this to {@code true} when desired.
* @since 4.1
*/
boolean
lazyInit
()
default
false
;
/**
* Declares the type filter to be used as an {@linkplain ComponentScan#includeFilters()
...
...
spring-context/src/main/java/org/springframework/context/annotation/ComponentScanAnnotationParser.java
浏览文件 @
f2e4ad23
/*
* 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.
...
...
@@ -106,6 +106,11 @@ class ComponentScanAnnotationParser {
}
}
boolean
lazyInit
=
componentScan
.
getBoolean
(
"lazyInit"
);
if
(
lazyInit
)
{
scanner
.
getBeanDefinitionDefaults
().
setLazyInit
(
true
);
}
List
<
String
>
basePackages
=
new
ArrayList
<
String
>();
for
(
String
pkg
:
componentScan
.
getStringArray
(
"value"
))
{
if
(
StringUtils
.
hasText
(
pkg
))
{
...
...
spring-context/src/test/java/org/springframework/context/annotation/ComponentScanAnnotationIntegrationTests.java
浏览文件 @
f2e4ad23
...
...
@@ -23,7 +23,17 @@ import java.lang.annotation.RetentionPolicy;
import
java.lang.annotation.Target
;
import
java.util.HashSet
;
import
example.scannable.CustomComponent
;
import
example.scannable.CustomStereotype
;
import
example.scannable.DefaultNamedComponent
;
import
example.scannable.FooService
;
import
example.scannable.MessageBean
;
import
example.scannable.ScopedProxyTestBean
;
import
example.scannable_implicitbasepackage.ComponentScanAnnotatedConfigWithImplicitBasePackage
;
import
example.scannable_scoped.CustomScopeAnnotationBean
;
import
example.scannable_scoped.MyScope
;
import
org.junit.Test
;
import
org.springframework.aop.support.AopUtils
;
import
org.springframework.beans.factory.annotation.CustomAutowireConfigurer
;
import
org.springframework.beans.factory.config.BeanDefinition
;
...
...
@@ -36,16 +46,6 @@ import org.springframework.context.support.GenericApplicationContext;
import
org.springframework.tests.context.SimpleMapScope
;
import
org.springframework.util.SerializationTestUtils
;
import
example.scannable.CustomComponent
;
import
example.scannable.CustomStereotype
;
import
example.scannable.DefaultNamedComponent
;
import
example.scannable.FooService
;
import
example.scannable.MessageBean
;
import
example.scannable.ScopedProxyTestBean
;
import
example.scannable_implicitbasepackage.ComponentScanAnnotatedConfigWithImplicitBasePackage
;
import
example.scannable_scoped.CustomScopeAnnotationBean
;
import
example.scannable_scoped.MyScope
;
import
static
org
.
hamcrest
.
CoreMatchers
.*;
import
static
org
.
junit
.
Assert
.*;
import
static
org
.
springframework
.
beans
.
factory
.
support
.
BeanDefinitionBuilder
.*;
...
...
@@ -116,10 +116,10 @@ public class ComponentScanAnnotationIntegrationTests {
ctx
.
getBean
(
ComposedAnnotationConfig
.
class
);
ctx
.
getBean
(
SimpleComponent
.
class
);
assertThat
(
"config class bean not found"
,
ctx
.
containsBeanDefinition
(
"componentScanAnnotationIntegrationTests.ComposedAnnotationConfig"
),
is
(
true
));
assertThat
(
"@ComponentScan annotated @Configuration class registered directly against "
+
"AnnotationConfigApplicationContext did not trigger component scanning as expected"
,
ctx
.
containsBean
(
"simpleComponent"
),
is
(
true
));
ctx
.
containsBeanDefinition
(
"componentScanAnnotationIntegrationTests.ComposedAnnotationConfig"
),
is
(
true
));
assertThat
(
"@ComponentScan annotated @Configuration class registered directly against "
+
"AnnotationConfigApplicationContext did not trigger component scanning as expected"
,
ctx
.
containsBean
(
"simpleComponent"
),
is
(
true
));
}
@Test
...
...
@@ -159,7 +159,8 @@ public class ComponentScanAnnotationIntegrationTests {
@Test
public
void
withCustomTypeFilter
()
{
AnnotationConfigApplicationContext
ctx
=
new
AnnotationConfigApplicationContext
(
ComponentScanWithCustomTypeFilter
.
class
);
KustomAnnotationAutowiredBean
testBean
=
ctx
.
getBean
(
KustomAnnotationAutowiredBean
.
class
);
assertFalse
(
ctx
.
getDefaultListableBeanFactory
().
containsSingleton
(
"kustomBean"
));
KustomAnnotationAutowiredBean
testBean
=
ctx
.
getBean
(
"kustomBean"
,
KustomAnnotationAutowiredBean
.
class
);
assertThat
(
testBean
.
getDependency
(),
notNullValue
());
}
...
...
@@ -294,7 +295,8 @@ class MyScopeMetadataResolver extends AnnotationScopeMetadataResolver {
useDefaultFilters
=
false
,
includeFilters
=
@Filter
(
type
=
FilterType
.
CUSTOM
,
value
=
ComponentScanParserTests
.
CustomTypeFilter
.
class
),
// exclude this class from scanning since it's in the scanned package
excludeFilters
=
@Filter
(
type
=
FilterType
.
ASSIGNABLE_TYPE
,
value
=
ComponentScanWithCustomTypeFilter
.
class
))
excludeFilters
=
@Filter
(
type
=
FilterType
.
ASSIGNABLE_TYPE
,
value
=
ComponentScanWithCustomTypeFilter
.
class
),
lazyInit
=
true
)
class
ComponentScanWithCustomTypeFilter
{
@Bean
@SuppressWarnings
({
"rawtypes"
,
"serial"
,
"unchecked"
})
...
...
spring-context/src/test/java/org/springframework/context/annotation/ComponentScanAnnotationTests.java
浏览文件 @
f2e4ad23
/*
* Copyright 2002-201
0
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.
...
...
@@ -17,6 +17,7 @@
package
org.springframework.context.annotation
;
import
org.junit.Test
;
import
org.springframework.beans.factory.support.DefaultBeanNameGenerator
;
import
org.springframework.context.annotation.ComponentScan.Filter
;
import
org.springframework.core.type.filter.TypeFilter
;
...
...
@@ -29,6 +30,7 @@ import org.springframework.core.type.filter.TypeFilter;
* @see ComponentScanAnnotationIntegrationTests
*/
public
class
ComponentScanAnnotationTests
{
@Test
public
void
noop
()
{
// no-op; the @ComponentScan-annotated MyConfig class below simply excercises
...
...
@@ -36,7 +38,9 @@ public class ComponentScanAnnotationTests {
}
}
@interface
MyAnnotation
{
}
@interface
MyAnnotation
{
}
@Configuration
@ComponentScan
(
...
...
@@ -44,18 +48,19 @@ public class ComponentScanAnnotationTests {
nameGenerator
=
DefaultBeanNameGenerator
.
class
,
scopedProxy
=
ScopedProxyMode
.
NO
,
scopeResolver
=
AnnotationScopeMetadataResolver
.
class
,
useDefaultFilters
=
false
,
resourcePattern
=
"**/*custom.class"
,
useDefaultFilters
=
false
,
includeFilters
=
{
@Filter
(
type
=
FilterType
.
ANNOTATION
,
value
=
MyAnnotation
.
class
)
},
excludeFilters
=
{
@Filter
(
type
=
FilterType
.
CUSTOM
,
value
=
TypeFilter
.
class
)
}
},
lazyInit
=
true
)
class
MyConfig
{
}
@ComponentScan
(
basePackageClasses
=
example
.
scannable
.
NamedComponent
.
class
)
class
SimpleConfig
{
}
\ No newline at end of file
class
SimpleConfig
{
}
spring-context/src/test/java/org/springframework/context/annotation/ComponentScanParserTests.java
浏览文件 @
f2e4ad23
...
...
@@ -21,7 +21,10 @@ import java.lang.annotation.Retention;
import
java.lang.annotation.RetentionPolicy
;
import
java.lang.annotation.Target
;
import
example.profilescan.ProfileAnnotatedComponent
;
import
example.scannable.AutowiredQualifierFooService
;
import
org.junit.Test
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.ConfigurableApplicationContext
;
import
org.springframework.context.support.ClassPathXmlApplicationContext
;
...
...
@@ -29,9 +32,7 @@ import org.springframework.context.support.GenericXmlApplicationContext;
import
org.springframework.core.type.classreading.MetadataReader
;
import
org.springframework.core.type.classreading.MetadataReaderFactory
;
import
org.springframework.core.type.filter.TypeFilter
;
import
example.profilescan.ProfileAnnotatedComponent
;
import
example.scannable.AutowiredQualifierFooService
;
import
org.springframework.stereotype.Component
;
import
static
org
.
hamcrest
.
CoreMatchers
.*;
import
static
org
.
junit
.
Assert
.*;
...
...
@@ -134,13 +135,13 @@ public class ComponentScanParserTests {
* Intentionally spelling "custom" with a "k" since there are numerous
* classes in this package named *Custom*.
*/
@Component
(
"kustomBean"
)
public
static
class
KustomAnnotationAutowiredBean
{
@Autowired
@CustomAnnotation
private
KustomAnnotationDependencyBean
dependency
;
public
KustomAnnotationDependencyBean
getDependency
()
{
return
this
.
dependency
;
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录