Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
ffdff1cd
Commit
ffdff1cd
authored
Oct 13, 2017
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Consider multiple MBeanExporters when excluding beans from export
Closes gh-10632
parent
8a653d2c
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
66 additions
and
15 deletions
+66
-15
SpringApplicationAdminJmxAutoConfiguration.java
...ure/admin/SpringApplicationAdminJmxAutoConfiguration.java
+9
-5
JndiDataSourceAutoConfiguration.java
...t/autoconfigure/jdbc/JndiDataSourceAutoConfiguration.java
+3
-7
SpringApplicationAdminJmxAutoConfigurationTests.java
...dmin/SpringApplicationAdminJmxAutoConfigurationTests.java
+20
-2
JndiDataSourceAutoConfigurationTests.java
...oconfigure/jdbc/JndiDataSourceAutoConfigurationTests.java
+34
-1
No files found.
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfiguration.java
View file @
ffdff1cd
...
...
@@ -16,6 +16,8 @@
package
org
.
springframework
.
boot
.
autoconfigure
.
admin
;
import
java.util.List
;
import
javax.management.MalformedObjectNameException
;
import
org.springframework.beans.factory.ObjectProvider
;
...
...
@@ -55,13 +57,13 @@ public class SpringApplicationAdminJmxAutoConfiguration {
*/
private
static
final
String
DEFAULT_JMX_NAME
=
"org.springframework.boot:type=Admin,name=SpringApplication"
;
private
final
MBeanExporter
mbeanExporter
;
private
final
List
<
MBeanExporter
>
mbeanExporters
;
private
final
Environment
environment
;
public
SpringApplicationAdminJmxAutoConfiguration
(
ObjectProvider
<
MBeanExporter
>
mbeanExporter
,
Environment
environment
)
{
this
.
mbeanExporter
=
mbeanExporter
.
getIfAvailable
();
ObjectProvider
<
List
<
MBeanExporter
>>
mbeanExporters
,
Environment
environment
)
{
this
.
mbeanExporter
s
=
mbeanExporters
.
getIfAvailable
();
this
.
environment
=
environment
;
}
...
...
@@ -71,8 +73,10 @@ public class SpringApplicationAdminJmxAutoConfiguration {
throws
MalformedObjectNameException
{
String
jmxName
=
this
.
environment
.
getProperty
(
JMX_NAME_PROPERTY
,
DEFAULT_JMX_NAME
);
if
(
this
.
mbeanExporter
!=
null
)
{
// Make sure to not register that MBean twice
this
.
mbeanExporter
.
addExcludedBean
(
jmxName
);
if
(
this
.
mbeanExporters
!=
null
)
{
// Make sure to not register that MBean twice
for
(
MBeanExporter
mbeanExporter
:
this
.
mbeanExporters
)
{
mbeanExporter
.
addExcludedBean
(
jmxName
);
}
}
return
new
SpringApplicationAdminMXBeanRegistrar
(
jmxName
);
}
...
...
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfiguration.java
View file @
ffdff1cd
/*
* Copyright 2012-201
6
the original author or authors.
* Copyright 2012-201
7
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.
...
...
@@ -18,7 +18,6 @@ package org.springframework.boot.autoconfigure.jdbc;
import
javax.sql.DataSource
;
import
org.springframework.beans.factory.NoSuchBeanDefinitionException
;
import
org.springframework.boot.autoconfigure.AutoConfigureBefore
;
import
org.springframework.boot.autoconfigure.EnableAutoConfiguration
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
...
...
@@ -65,15 +64,12 @@ public class JndiDataSourceAutoConfiguration {
}
private
void
excludeMBeanIfNecessary
(
Object
candidate
,
String
beanName
)
{
try
{
MBeanExporter
mbeanExporter
=
this
.
context
.
getBean
(
MBeanExporter
.
class
);
for
(
MBeanExporter
mbeanExporter
:
this
.
context
.
getBeansOfType
(
MBeanExporter
.
class
).
values
())
{
if
(
JmxUtils
.
isMBean
(
candidate
.
getClass
()))
{
mbeanExporter
.
addExcludedBean
(
beanName
);
}
}
catch
(
NoSuchBeanDefinitionException
ex
)
{
// No exporter. Exclusion is unnecessary
}
}
}
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfigurationTests.java
View file @
ffdff1cd
/*
* Copyright 2012-201
6
the original author or authors.
* Copyright 2012-201
7
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.
...
...
@@ -42,6 +42,9 @@ import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
import
org.springframework.boot.test.util.EnvironmentTestUtils
;
import
org.springframework.context.ConfigurableApplicationContext
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.jmx.export.MBeanExporter
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
junit
.
Assert
.
fail
;
...
...
@@ -186,10 +189,25 @@ public class SpringApplicationAdminJmxAutoConfigurationTests {
private
void
load
(
String
...
environment
)
{
AnnotationConfigApplicationContext
applicationContext
=
new
AnnotationConfigApplicationContext
();
EnvironmentTestUtils
.
addEnvironment
(
applicationContext
,
environment
);
applicationContext
.
register
(
JmxAuto
Configuration
.
class
,
applicationContext
.
register
(
MultipleMBeanExporters
Configuration
.
class
,
SpringApplicationAdminJmxAutoConfiguration
.
class
);
applicationContext
.
refresh
();
this
.
context
=
applicationContext
;
}
@Configuration
static
class
MultipleMBeanExportersConfiguration
{
@Bean
public
MBeanExporter
firstMBeanExporter
()
{
return
new
MBeanExporter
();
}
@Bean
public
MBeanExporter
secondMBeanExporter
()
{
return
new
MBeanExporter
();
}
}
}
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfigurationTests.java
View file @
ffdff1cd
/*
* Copyright 2012-201
6
the original author or authors.
* Copyright 2012-201
7
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.
...
...
@@ -117,6 +117,30 @@ public class JndiDataSourceAutoConfigurationTests {
assertThat
(
excludedBeans
).
containsExactly
(
"dataSource"
);
}
@SuppressWarnings
(
"unchecked"
)
@Test
public
void
mbeanDataSourceIsExcludedFromExportByAllExporters
()
throws
IllegalStateException
,
NamingException
{
DataSource
dataSource
=
new
BasicDataSource
();
configureJndi
(
"foo"
,
dataSource
);
this
.
context
=
new
AnnotationConfigApplicationContext
();
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
"spring.datasource.jndi-name:foo"
);
this
.
context
.
register
(
JndiDataSourceAutoConfiguration
.
class
,
MBeanExporterConfiguration
.
class
,
AnotherMBeanExporterConfiguration
.
class
);
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
getBean
(
DataSource
.
class
)).
isEqualTo
(
dataSource
);
for
(
MBeanExporter
exporter
:
this
.
context
.
getBeansOfType
(
MBeanExporter
.
class
)
.
values
())
{
Set
<
String
>
excludedBeans
=
(
Set
<
String
>)
new
DirectFieldAccessor
(
exporter
)
.
getPropertyValue
(
"excludedBeans"
);
assertThat
(
excludedBeans
).
containsExactly
(
"dataSource"
);
}
}
@SuppressWarnings
(
"unchecked"
)
@Test
public
void
standardDataSourceIsNotExcludedFromExport
()
...
...
@@ -152,4 +176,13 @@ public class JndiDataSourceAutoConfigurationTests {
}
private
static
class
AnotherMBeanExporterConfiguration
{
@Bean
MBeanExporter
anotherMbeanExporter
()
{
return
new
MBeanExporter
();
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment