Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
eddbf13d
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 搜索 >>
提交
eddbf13d
编写于
8月 09, 2018
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
BeanFactoryUtils caches transformedBeanName results for factory beans
Issue: SPR-17151
上级
6b6384a0
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
39 addition
and
21 deletion
+39
-21
spring-beans/src/main/java/org/springframework/beans/factory/BeanFactoryUtils.java
...a/org/springframework/beans/factory/BeanFactoryUtils.java
+17
-4
spring-beans/src/test/java/org/springframework/beans/factory/xml/XmlListableBeanFactoryTests.java
...mework/beans/factory/xml/XmlListableBeanFactoryTests.java
+22
-17
未找到文件。
spring-beans/src/main/java/org/springframework/beans/factory/BeanFactoryUtils.java
浏览文件 @
eddbf13d
...
@@ -22,6 +22,7 @@ import java.util.Arrays;
...
@@ -22,6 +22,7 @@ import java.util.Arrays;
import
java.util.LinkedHashMap
;
import
java.util.LinkedHashMap
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.concurrent.ConcurrentHashMap
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.BeansException
;
import
org.springframework.core.ResolvableType
;
import
org.springframework.core.ResolvableType
;
...
@@ -51,6 +52,13 @@ public abstract class BeanFactoryUtils {
...
@@ -51,6 +52,13 @@ public abstract class BeanFactoryUtils {
*/
*/
public
static
final
String
GENERATED_BEAN_NAME_SEPARATOR
=
"#"
;
public
static
final
String
GENERATED_BEAN_NAME_SEPARATOR
=
"#"
;
/**
* Cache from name with factory bean prefix to stripped name without dereference.
* @since 5.1
* @see BeanFactory#FACTORY_BEAN_PREFIX
*/
private
static
final
Map
<
String
,
String
>
transformedBeanNameCache
=
new
ConcurrentHashMap
<>();
/**
/**
* Return whether the given name is a factory dereference
* Return whether the given name is a factory dereference
...
@@ -72,11 +80,16 @@ public abstract class BeanFactoryUtils {
...
@@ -72,11 +80,16 @@ public abstract class BeanFactoryUtils {
*/
*/
public
static
String
transformedBeanName
(
String
name
)
{
public
static
String
transformedBeanName
(
String
name
)
{
Assert
.
notNull
(
name
,
"'name' must not be null"
);
Assert
.
notNull
(
name
,
"'name' must not be null"
);
String
beanName
=
name
;
if
(!
name
.
startsWith
(
BeanFactory
.
FACTORY_BEAN_PREFIX
))
{
while
(
beanName
.
startsWith
(
BeanFactory
.
FACTORY_BEAN_PREFIX
))
{
return
name
;
beanName
=
beanName
.
substring
(
BeanFactory
.
FACTORY_BEAN_PREFIX
.
length
());
}
}
return
beanName
;
return
transformedBeanNameCache
.
computeIfAbsent
(
name
,
beanName
->
{
do
{
beanName
=
beanName
.
substring
(
BeanFactory
.
FACTORY_BEAN_PREFIX
.
length
());
}
while
(
beanName
.
startsWith
(
BeanFactory
.
FACTORY_BEAN_PREFIX
));
return
beanName
;
});
}
}
/**
/**
...
...
spring-beans/src/test/java/org/springframework/beans/factory/xml/XmlListableBeanFactoryTests.java
浏览文件 @
eddbf13d
/*
/*
* Copyright 2002-201
5
the original author or authors.
* Copyright 2002-201
8
the original author or authors.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
...
@@ -42,30 +42,33 @@ import static org.junit.Assert.*;
...
@@ -42,30 +42,33 @@ import static org.junit.Assert.*;
* @author Juergen Hoeller
* @author Juergen Hoeller
* @since 09.11.2003
* @since 09.11.2003
*/
*/
@SuppressWarnings
({
"rawtypes"
,
"unchecked"
})
@SuppressWarnings
({
"rawtypes"
,
"unchecked"
})
public
class
XmlListableBeanFactoryTests
extends
AbstractListableBeanFactoryTests
{
public
class
XmlListableBeanFactoryTests
extends
AbstractListableBeanFactoryTests
{
private
DefaultListableBeanFactory
parent
;
private
DefaultListableBeanFactory
parent
;
private
DefaultListableBeanFactory
factory
;
private
DefaultListableBeanFactory
factory
;
@Before
@Before
public
void
set
U
p
()
{
public
void
set
u
p
()
{
parent
=
new
DefaultListableBeanFactory
();
parent
=
new
DefaultListableBeanFactory
();
Map
m
=
new
HashMap
();
m
.
put
(
"name"
,
"Albert"
);
Map
map
=
new
HashMap
();
map
.
put
(
"name"
,
"Albert"
);
RootBeanDefinition
bd1
=
new
RootBeanDefinition
(
TestBean
.
class
);
RootBeanDefinition
bd1
=
new
RootBeanDefinition
(
TestBean
.
class
);
bd1
.
setPropertyValues
(
new
MutablePropertyValues
(
m
));
bd1
.
setPropertyValues
(
new
MutablePropertyValues
(
m
ap
));
parent
.
registerBeanDefinition
(
"father"
,
bd1
);
parent
.
registerBeanDefinition
(
"father"
,
bd1
);
m
=
new
HashMap
();
m
.
put
(
"name"
,
"Roderick"
);
map
=
new
HashMap
();
map
.
put
(
"name"
,
"Roderick"
);
RootBeanDefinition
bd2
=
new
RootBeanDefinition
(
TestBean
.
class
);
RootBeanDefinition
bd2
=
new
RootBeanDefinition
(
TestBean
.
class
);
bd2
.
setPropertyValues
(
new
MutablePropertyValues
(
m
));
bd2
.
setPropertyValues
(
new
MutablePropertyValues
(
m
ap
));
parent
.
registerBeanDefinition
(
"rod"
,
bd2
);
parent
.
registerBeanDefinition
(
"rod"
,
bd2
);
this
.
factory
=
new
DefaultListableBeanFactory
(
parent
);
this
.
factory
=
new
DefaultListableBeanFactory
(
parent
);
new
XmlBeanDefinitionReader
(
this
.
factory
).
loadBeanDefinitions
(
new
XmlBeanDefinitionReader
(
this
.
factory
).
loadBeanDefinitions
(
new
ClassPathResource
(
"test.xml"
,
getClass
()));
new
ClassPathResource
(
"test.xml"
,
getClass
()));
this
.
factory
.
addBeanPostProcessor
(
new
BeanPostProcessor
()
{
this
.
factory
.
addBeanPostProcessor
(
new
BeanPostProcessor
()
{
@Override
@Override
public
Object
postProcessBeforeInitialization
(
Object
bean
,
String
name
)
throws
BeansException
{
public
Object
postProcessBeforeInitialization
(
Object
bean
,
String
name
)
throws
BeansException
{
...
@@ -82,9 +85,10 @@ public class XmlListableBeanFactoryTests extends AbstractListableBeanFactoryTest
...
@@ -82,9 +85,10 @@ public class XmlListableBeanFactoryTests extends AbstractListableBeanFactoryTest
return
bean
;
return
bean
;
}
}
});
});
this
.
factory
.
addBeanPostProcessor
(
new
LifecycleBean
.
PostProcessor
());
this
.
factory
.
addBeanPostProcessor
(
new
LifecycleBean
.
PostProcessor
());
this
.
factory
.
addBeanPostProcessor
(
new
ProtectedLifecycleBean
.
PostProcessor
());
this
.
factory
.
addBeanPostProcessor
(
new
ProtectedLifecycleBean
.
PostProcessor
());
//this.factory.preInstantiateSingletons();
//
this.factory.preInstantiateSingletons();
}
}
@Override
@Override
...
@@ -92,6 +96,7 @@ public class XmlListableBeanFactoryTests extends AbstractListableBeanFactoryTest
...
@@ -92,6 +96,7 @@ public class XmlListableBeanFactoryTests extends AbstractListableBeanFactoryTest
return
factory
;
return
factory
;
}
}
@Test
@Test
@Override
@Override
public
void
count
()
{
public
void
count
()
{
...
@@ -104,19 +109,19 @@ public class XmlListableBeanFactoryTests extends AbstractListableBeanFactoryTest
...
@@ -104,19 +109,19 @@ public class XmlListableBeanFactoryTests extends AbstractListableBeanFactoryTest
}
}
@Test
@Test
public
void
lifecycleMethods
()
throws
Exception
{
public
void
lifecycleMethods
()
{
LifecycleBean
bean
=
(
LifecycleBean
)
getBeanFactory
().
getBean
(
"lifecycle"
);
LifecycleBean
bean
=
(
LifecycleBean
)
getBeanFactory
().
getBean
(
"lifecycle"
);
bean
.
businessMethod
();
bean
.
businessMethod
();
}
}
@Test
@Test
public
void
protectedLifecycleMethods
()
throws
Exception
{
public
void
protectedLifecycleMethods
()
{
ProtectedLifecycleBean
bean
=
(
ProtectedLifecycleBean
)
getBeanFactory
().
getBean
(
"protectedLifecycle"
);
ProtectedLifecycleBean
bean
=
(
ProtectedLifecycleBean
)
getBeanFactory
().
getBean
(
"protectedLifecycle"
);
bean
.
businessMethod
();
bean
.
businessMethod
();
}
}
@Test
@Test
public
void
descriptionButNoProperties
()
throws
Exception
{
public
void
descriptionButNoProperties
()
{
TestBean
validEmpty
=
(
TestBean
)
getBeanFactory
().
getBean
(
"validEmptyWithDescription"
);
TestBean
validEmpty
=
(
TestBean
)
getBeanFactory
().
getBean
(
"validEmptyWithDescription"
);
assertEquals
(
0
,
validEmpty
.
getAge
());
assertEquals
(
0
,
validEmpty
.
getAge
());
}
}
...
@@ -125,7 +130,7 @@ public class XmlListableBeanFactoryTests extends AbstractListableBeanFactoryTest
...
@@ -125,7 +130,7 @@ public class XmlListableBeanFactoryTests extends AbstractListableBeanFactoryTest
* Test that properties with name as well as id creating an alias up front.
* Test that properties with name as well as id creating an alias up front.
*/
*/
@Test
@Test
public
void
autoAliasing
()
throws
Exception
{
public
void
autoAliasing
()
{
List
beanNames
=
Arrays
.
asList
(
getListableBeanFactory
().
getBeanDefinitionNames
());
List
beanNames
=
Arrays
.
asList
(
getListableBeanFactory
().
getBeanDefinitionNames
());
TestBean
tb1
=
(
TestBean
)
getBeanFactory
().
getBean
(
"aliased"
);
TestBean
tb1
=
(
TestBean
)
getBeanFactory
().
getBean
(
"aliased"
);
...
@@ -224,7 +229,7 @@ public class XmlListableBeanFactoryTests extends AbstractListableBeanFactoryTest
...
@@ -224,7 +229,7 @@ public class XmlListableBeanFactoryTests extends AbstractListableBeanFactoryTest
}
}
@Test
@Test
public
void
beanPostProcessor
()
throws
Exception
{
public
void
beanPostProcessor
()
{
TestBean
kerry
=
(
TestBean
)
getBeanFactory
().
getBean
(
"kerry"
);
TestBean
kerry
=
(
TestBean
)
getBeanFactory
().
getBean
(
"kerry"
);
TestBean
kathy
=
(
TestBean
)
getBeanFactory
().
getBean
(
"kathy"
);
TestBean
kathy
=
(
TestBean
)
getBeanFactory
().
getBean
(
"kathy"
);
DummyFactory
factory
=
(
DummyFactory
)
getBeanFactory
().
getBean
(
"&singletonFactory"
);
DummyFactory
factory
=
(
DummyFactory
)
getBeanFactory
().
getBean
(
"&singletonFactory"
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录