Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
dc6c1dc7
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,发现更多精彩内容 >>
提交
dc6c1dc7
编写于
12月 13, 2009
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
MBeanExporter detects FactoryBean-exported resources independent from declaration order
上级
db6e8074
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
120 addition
and
38 deletion
+120
-38
org.springframework.context/src/main/java/org/springframework/jmx/export/MBeanExporter.java
...in/java/org/springframework/jmx/export/MBeanExporter.java
+35
-32
org.springframework.context/src/test/java/org/springframework/jmx/export/annotation/AnnotationLazyInitMBeanTests.java
...k/jmx/export/annotation/AnnotationLazyInitMBeanTests.java
+5
-0
org.springframework.context/src/test/java/org/springframework/jmx/export/annotation/AnnotationTestBeanFactory.java
...work/jmx/export/annotation/AnnotationTestBeanFactory.java
+45
-0
org.springframework.context/src/test/java/org/springframework/jmx/export/annotation/FactoryCreatedAnnotationTestBean.java
...x/export/annotation/FactoryCreatedAnnotationTestBean.java
+25
-0
org.springframework.context/src/test/java/org/springframework/jmx/export/annotation/lazyAssembling.xml
.../springframework/jmx/export/annotation/lazyAssembling.xml
+8
-4
org.springframework.context/src/test/java/org/springframework/jmx/export/annotation/lazyNaming.xml
.../org/springframework/jmx/export/annotation/lazyNaming.xml
+2
-2
未找到文件。
org.springframework.context/src/main/java/org/springframework/jmx/export/MBeanExporter.java
浏览文件 @
dc6c1dc7
...
...
@@ -21,6 +21,7 @@ import java.util.Arrays;
import
java.util.HashMap
;
import
java.util.HashSet
;
import
java.util.LinkedHashMap
;
import
java.util.LinkedHashSet
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
...
...
@@ -43,11 +44,11 @@ import org.springframework.aop.target.LazyInitTargetSource;
import
org.springframework.beans.factory.BeanClassLoaderAware
;
import
org.springframework.beans.factory.BeanFactory
;
import
org.springframework.beans.factory.BeanFactoryAware
;
import
org.springframework.beans.factory.CannotLoadBeanClassException
;
import
org.springframework.beans.factory.DisposableBean
;
import
org.springframework.beans.factory.InitializingBean
;
import
org.springframework.beans.factory.ListableBeanFactory
;
import
org.springframework.beans.factory.NoSuchBeanDefinitionException
;
import
org.springframework.beans.factory.config.BeanDefinition
;
import
org.springframework.beans.factory.config.ConfigurableBeanFactory
;
import
org.springframework.beans.factory.config.ConfigurableListableBeanFactory
;
import
org.springframework.core.Constants
;
import
org.springframework.jmx.export.assembler.AutodetectCapableMBeanInfoAssembler
;
...
...
@@ -142,7 +143,7 @@ public class MBeanExporter extends MBeanRegistrationSupport
/** The autodetect mode to use for this MBeanExporter */
private
Integer
autodetectMode
;
/** Whether to eagerly init candidate beans when autodetecting MBeans */
/** Whether to eagerly init
ialize
candidate beans when autodetecting MBeans */
private
boolean
allowEagerInit
=
false
;
/** Indicates whether Spring should modify generated ObjectNames */
...
...
@@ -381,14 +382,13 @@ public class MBeanExporter extends MBeanRegistrationSupport
* <code>ListableBeanFactory</code> is required).
* @see #setBeans
* @see #setAutodetect
* @throws IllegalArgumentException if the supplied <code>beanFactory</code> is not a {@link ListableBeanFactory}.
*/
public
void
setBeanFactory
(
BeanFactory
beanFactory
)
{
if
(
beanFactory
instanceof
ListableBeanFactory
)
{
this
.
beanFactory
=
(
ListableBeanFactory
)
beanFactory
;
}
else
{
logger
.
info
(
"MBeanExporter not running in a ListableBeanFactory:
A
utodetection of MBeans not available."
);
logger
.
info
(
"MBeanExporter not running in a ListableBeanFactory:
a
utodetection of MBeans not available."
);
}
}
...
...
@@ -537,17 +537,8 @@ public class MBeanExporter extends MBeanRegistrationSupport
* @see org.springframework.beans.factory.config.BeanDefinition#isLazyInit
*/
protected
boolean
isBeanDefinitionLazyInit
(
ListableBeanFactory
beanFactory
,
String
beanName
)
{
if
(!(
beanFactory
instanceof
ConfigurableListableBeanFactory
))
{
return
false
;
}
try
{
BeanDefinition
bd
=
((
ConfigurableListableBeanFactory
)
beanFactory
).
getBeanDefinition
(
beanName
);
return
bd
.
isLazyInit
();
}
catch
(
NoSuchBeanDefinitionException
ex
)
{
// Probably a directly registered singleton.
return
false
;
}
return
(
beanFactory
instanceof
ConfigurableListableBeanFactory
&&
beanFactory
.
containsBeanDefinition
(
beanName
)
&&
((
ConfigurableListableBeanFactory
)
beanFactory
).
getBeanDefinition
(
beanName
).
isLazyInit
());
}
/**
...
...
@@ -594,7 +585,7 @@ public class MBeanExporter extends MBeanRegistrationSupport
// Plain bean instance -> register it directly.
if
(
this
.
beanFactory
!=
null
)
{
Map
<
String
,
?>
beansOfSameType
=
this
.
beanFactory
.
getBeansOfType
(
mapValue
.
getClass
(),
false
,
false
);
this
.
beanFactory
.
getBeansOfType
(
mapValue
.
getClass
(),
false
,
this
.
allowEagerInit
);
for
(
Map
.
Entry
<
String
,
?>
entry
:
beansOfSameType
.
entrySet
())
{
if
(
entry
.
getValue
()
==
mapValue
)
{
String
beanName
=
entry
.
getKey
();
...
...
@@ -875,27 +866,39 @@ public class MBeanExporter extends MBeanRegistrationSupport
* whether to include a bean or not
*/
private
void
autodetect
(
AutodetectCallback
callback
)
{
String
[]
beanNames
=
this
.
beanFactory
.
getBeanNamesForType
(
Object
.
class
,
true
,
this
.
allowEagerInit
);
Set
<
String
>
beanNames
=
new
LinkedHashSet
<
String
>(
this
.
beanFactory
.
getBeanDefinitionCount
());
beanNames
.
addAll
(
Arrays
.
asList
(
this
.
beanFactory
.
getBeanDefinitionNames
()));
if
(
this
.
beanFactory
instanceof
ConfigurableBeanFactory
)
{
beanNames
.
addAll
(
Arrays
.
asList
(((
ConfigurableBeanFactory
)
this
.
beanFactory
).
getSingletonNames
()));
}
for
(
String
beanName
:
beanNames
)
{
if
(!
isExcluded
(
beanName
))
{
Class
beanClass
=
this
.
beanFactory
.
getType
(
beanName
);
if
(
beanClass
!=
null
&&
callback
.
include
(
beanClass
,
beanName
))
{
boolean
lazyInit
=
isBeanDefinitionLazyInit
(
this
.
beanFactory
,
beanName
);
Object
beanInstance
=
(!
lazyInit
?
this
.
beanFactory
.
getBean
(
beanName
)
:
null
);
if
(!
this
.
beans
.
containsValue
(
beanName
)
&&
(
beanInstance
==
null
||
!
CollectionUtils
.
containsInstance
(
this
.
beans
.
values
(),
beanInstance
)))
{
// Not already registered for JMX exposure.
this
.
beans
.
put
(
beanName
,
(
beanInstance
!=
null
?
beanInstance
:
beanName
));
if
(
logger
.
isInfoEnabled
())
{
logger
.
info
(
"Bean with name '"
+
beanName
+
"' has been autodetected for JMX exposure"
);
try
{
Class
beanClass
=
this
.
beanFactory
.
getType
(
beanName
);
if
(
beanClass
!=
null
&&
callback
.
include
(
beanClass
,
beanName
))
{
boolean
lazyInit
=
isBeanDefinitionLazyInit
(
this
.
beanFactory
,
beanName
);
Object
beanInstance
=
(!
lazyInit
?
this
.
beanFactory
.
getBean
(
beanName
)
:
null
);
if
(!
this
.
beans
.
containsValue
(
beanName
)
&&
(
beanInstance
==
null
||
!
CollectionUtils
.
containsInstance
(
this
.
beans
.
values
(),
beanInstance
)))
{
// Not already registered for JMX exposure.
this
.
beans
.
put
(
beanName
,
(
beanInstance
!=
null
?
beanInstance
:
beanName
));
if
(
logger
.
isInfoEnabled
())
{
logger
.
info
(
"Bean with name '"
+
beanName
+
"' has been autodetected for JMX exposure"
);
}
}
}
else
{
if
(
logger
.
isDebugEnabled
())
{
logger
.
debug
(
"Bean with name '"
+
beanName
+
"' is already registered for JMX exposure"
);
else
{
if
(
logger
.
isDebugEnabled
())
{
logger
.
debug
(
"Bean with name '"
+
beanName
+
"' is already registered for JMX exposure"
);
}
}
}
}
catch
(
CannotLoadBeanClassException
ex
)
{
if
(
this
.
allowEagerInit
)
{
throw
ex
;
}
// otherwise ignore beans where the class is not resolvable
}
}
}
}
...
...
org.springframework.context/src/test/java/org/springframework/jmx/export/annotation/AnnotationLazyInitMBeanTests.java
浏览文件 @
dc6c1dc7
...
...
@@ -57,6 +57,11 @@ public class AnnotationLazyInitMBeanTests extends TestCase {
String
name
=
(
String
)
server
.
getAttribute
(
oname
,
"Name"
);
assertEquals
(
"Invalid name returned"
,
"TEST"
,
name
);
oname
=
ObjectNameManager
.
getInstance
(
"bean:name=testBean5"
);
assertNotNull
(
server
.
getObjectInstance
(
oname
));
name
=
(
String
)
server
.
getAttribute
(
oname
,
"Name"
);
assertEquals
(
"Invalid name returned"
,
"FACTORY"
,
name
);
oname
=
ObjectNameManager
.
getInstance
(
"spring:mbean=true"
);
assertNotNull
(
server
.
getObjectInstance
(
oname
));
name
=
(
String
)
server
.
getAttribute
(
oname
,
"Name"
);
...
...
org.springframework.context/src/test/java/org/springframework/jmx/export/annotation/AnnotationTestBeanFactory.java
0 → 100644
浏览文件 @
dc6c1dc7
/*
* Copyright 2002-2009 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.jmx.export.annotation
;
import
org.springframework.beans.factory.FactoryBean
;
import
org.springframework.jmx.IJmxTestBean
;
/**
* @author Juergen Hoeller
*/
public
class
AnnotationTestBeanFactory
implements
FactoryBean
<
IJmxTestBean
>
{
private
final
FactoryCreatedAnnotationTestBean
instance
=
new
FactoryCreatedAnnotationTestBean
();
public
AnnotationTestBeanFactory
()
{
this
.
instance
.
setName
(
"FACTORY"
);
}
public
IJmxTestBean
getObject
()
throws
Exception
{
return
this
.
instance
;
}
public
Class
<?
extends
IJmxTestBean
>
getObjectType
()
{
return
FactoryCreatedAnnotationTestBean
.
class
;
}
public
boolean
isSingleton
()
{
return
true
;
}
}
org.springframework.context/src/test/java/org/springframework/jmx/export/annotation/FactoryCreatedAnnotationTestBean.java
0 → 100644
浏览文件 @
dc6c1dc7
/*
* Copyright 2002-2009 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.jmx.export.annotation
;
/**
* @author Juergen Hoeller
*/
@ManagedResource
(
objectName
=
"bean:name=testBean5"
)
public
class
FactoryCreatedAnnotationTestBean
extends
AnnotationTestBean
{
}
org.springframework.context/src/test/java/org/springframework/jmx/export/annotation/lazyAssembling.xml
浏览文件 @
dc6c1dc7
...
...
@@ -5,21 +5,25 @@
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
>
<context:mbean-export
server=
"server"
registration=
"replaceExisting"
/>
<context:mbean-export
server=
"server"
registration=
"replaceExisting"
/>
<context:mbean-export
server=
"server"
registration=
"replaceExisting"
/>
<context:mbean-export
server=
"server"
registration=
"replaceExisting"
/>
<bean
id=
"server"
class=
"org.springframework.jmx.support.MBeanServerFactoryBean"
/>
<bean
id=
"server"
class=
"org.springframework.jmx.support.MBeanServerFactoryBean"
/>
<bean
name=
"bean:name=testBean4"
class=
"org.springframework.jmx.export.annotation.AnnotationTestBean"
lazy-init=
"true"
>
<bean
name=
"bean:name=testBean4"
class=
"org.springframework.jmx.export.annotation.AnnotationTestBean"
lazy-init=
"true"
>
<property
name=
"name"
value=
"TEST"
/>
<property
name=
"age"
value=
"100"
/>
</bean>
<bean
name=
"bean:name=testBean5"
class=
"org.springframework.jmx.export.annotation.AnnotationTestBeanFactory"
/>
<bean
name=
"spring:mbean=true"
class=
"org.springframework.jmx.export.TestDynamicMBean"
lazy-init=
"true"
/>
<bean
name=
"spring:mbean=another"
class=
"org.springframework.jmx.export.MBeanExporterTests$Person"
lazy-init=
"true"
>
<property
name=
"name"
value=
"Juergen Hoeller"
/>
</bean>
<bean
id=
"notLoadable"
class=
"does.not.exist"
lazy-init=
"true"
/>
</beans>
org.springframework.context/src/test/java/org/springframework/jmx/export/annotation/lazyNaming.xml
浏览文件 @
dc6c1dc7
...
...
@@ -5,9 +5,9 @@
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
>
<context:mbean-export
server=
"server"
/>
<context:mbean-export
server=
"server"
/>
<bean
id=
"server"
class=
"org.springframework.jmx.support.MBeanServerFactoryBean"
/>
<bean
id=
"server"
class=
"org.springframework.jmx.support.MBeanServerFactoryBean"
/>
<bean
id=
"testBean"
class=
"org.springframework.jmx.export.annotation.AnnotationTestBean"
lazy-init=
"true"
>
<property
name=
"name"
value=
"TEST"
/>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录