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
a6b30a3a
Commit
a6b30a3a
authored
Sep 05, 2017
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reflect context hierarchy in beans endpoint’s response structure
Closes gh-10156
parent
ab548011
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
58 additions
and
79 deletions
+58
-79
BeansEndpoint.java
.../springframework/boot/actuate/endpoint/BeansEndpoint.java
+41
-29
BeansEndpointTests.java
...ngframework/boot/actuate/endpoint/BeansEndpointTests.java
+15
-46
SampleActuatorApplicationTests.java
.../java/sample/actuator/SampleActuatorApplicationTests.java
+2
-4
No files found.
spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/BeansEndpoint.java
View file @
a6b30a3a
...
@@ -16,8 +16,6 @@
...
@@ -16,8 +16,6 @@
package
org
.
springframework
.
boot
.
actuate
.
endpoint
;
package
org
.
springframework
.
boot
.
actuate
.
endpoint
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
...
@@ -32,7 +30,7 @@ import org.springframework.context.ConfigurableApplicationContext;
...
@@ -32,7 +30,7 @@ import org.springframework.context.ConfigurableApplicationContext;
import
org.springframework.util.StringUtils
;
import
org.springframework.util.StringUtils
;
/**
/**
* {@link Endpoint} to expose details of an application's bean, grouped by application
* {@link Endpoint} to expose details of an application's bean
s
, grouped by application
* context.
* context.
*
*
* @author Dave Syer
* @author Dave Syer
...
@@ -55,50 +53,53 @@ public class BeansEndpoint {
...
@@ -55,50 +53,53 @@ public class BeansEndpoint {
}
}
@ReadOperation
@ReadOperation
public
Map
<
String
,
Object
>
beans
()
{
public
ApplicationContextDescriptor
beans
()
{
List
<
ApplicationContextDescriptor
>
contexts
=
new
ArrayList
<>();
return
ApplicationContextDescriptor
.
describing
(
this
.
context
);
ConfigurableApplicationContext
current
=
this
.
context
;
while
(
current
!=
null
)
{
contexts
.
add
(
ApplicationContextDescriptor
.
describing
(
current
));
current
=
getConfigurableParent
(
current
);
}
return
Collections
.
singletonMap
(
"contexts"
,
contexts
);
}
}
private
ConfigurableApplicationContext
getConfigurableParent
(
/**
ConfigurableApplicationContext
context
)
{
* Response produced by the {@link BeansEndpoint}, primarily intended for
ApplicationContext
parent
=
context
.
getParent
();
* serialization to JSON.
if
(
parent
instanceof
ConfigurableApplicationContext
)
{
*/
return
(
ConfigurableApplicationContext
)
parent
;
public
static
class
BeansEndpointResponse
{
private
List
<
ApplicationContextDescriptor
>
contexts
;
public
BeansEndpointResponse
(
List
<
ApplicationContextDescriptor
>
contexts
)
{
this
.
contexts
=
contexts
;
}
}
return
null
;
public
List
<
ApplicationContextDescriptor
>
getContexts
()
{
return
this
.
contexts
;
}
}
}
/**
/**
* A description of an application context, primarily intended for serialization to
* A description of an application context, primarily intended for serialization to
* JSON.
* JSON.
*/
*/
static
final
class
ApplicationContextDescriptor
{
public
static
final
class
ApplicationContextDescriptor
{
private
final
String
id
;
private
final
String
id
;
private
final
String
parentId
;
private
final
Map
<
String
,
BeanDescriptor
>
beans
;
private
final
Map
<
String
,
BeanDescriptor
>
beans
;
private
ApplicationContextDescriptor
(
String
id
,
String
parentId
,
private
final
ApplicationContextDescriptor
parent
;
Map
<
String
,
BeanDescriptor
>
beans
)
{
private
ApplicationContextDescriptor
(
String
id
,
Map
<
String
,
BeanDescriptor
>
beans
,
ApplicationContextDescriptor
parent
)
{
this
.
id
=
id
;
this
.
id
=
id
;
this
.
parentId
=
parentId
;
this
.
beans
=
beans
;
this
.
beans
=
beans
;
this
.
parent
=
parent
;
}
}
public
String
getId
()
{
public
String
getId
()
{
return
this
.
id
;
return
this
.
id
;
}
}
public
String
getParentId
()
{
public
ApplicationContextDescriptor
getParent
()
{
return
this
.
parent
Id
;
return
this
.
parent
;
}
}
public
Map
<
String
,
BeanDescriptor
>
getBeans
()
{
public
Map
<
String
,
BeanDescriptor
>
getBeans
()
{
...
@@ -107,10 +108,12 @@ public class BeansEndpoint {
...
@@ -107,10 +108,12 @@ public class BeansEndpoint {
private
static
ApplicationContextDescriptor
describing
(
private
static
ApplicationContextDescriptor
describing
(
ConfigurableApplicationContext
context
)
{
ConfigurableApplicationContext
context
)
{
ApplicationContext
parent
=
context
.
getParent
();
if
(
context
==
null
)
{
return
null
;
}
return
new
ApplicationContextDescriptor
(
context
.
getId
(),
return
new
ApplicationContextDescriptor
(
context
.
getId
(),
parent
==
null
?
null
:
parent
.
getId
(
),
describeBeans
(
context
.
getBeanFactory
()
),
describ
eBeans
(
context
.
getBeanFactory
(
)));
describ
ing
(
getConfigurableParent
(
context
)));
}
}
private
static
Map
<
String
,
BeanDescriptor
>
describeBeans
(
private
static
Map
<
String
,
BeanDescriptor
>
describeBeans
(
...
@@ -138,13 +141,22 @@ public class BeansEndpoint {
...
@@ -138,13 +141,22 @@ public class BeansEndpoint {
&&
(!
bd
.
isLazyInit
()
||
bf
.
containsSingleton
(
beanName
)));
&&
(!
bd
.
isLazyInit
()
||
bf
.
containsSingleton
(
beanName
)));
}
}
private
static
ConfigurableApplicationContext
getConfigurableParent
(
ConfigurableApplicationContext
context
)
{
ApplicationContext
parent
=
context
.
getParent
();
if
(
parent
instanceof
ConfigurableApplicationContext
)
{
return
(
ConfigurableApplicationContext
)
parent
;
}
return
null
;
}
}
}
/**
/**
* A description of a bean in an application context, primarily intended for
* A description of a bean in an application context, primarily intended for
* serialization to JSON.
* serialization to JSON.
*/
*/
static
final
class
BeanDescriptor
{
public
static
final
class
BeanDescriptor
{
private
final
String
[]
aliases
;
private
final
String
[]
aliases
;
...
...
spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/BeansEndpointTests.java
View file @
a6b30a3a
...
@@ -43,27 +43,22 @@ import static org.assertj.core.api.Assertions.assertThat;
...
@@ -43,27 +43,22 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
*/
public
class
BeansEndpointTests
{
public
class
BeansEndpointTests
{
@SuppressWarnings
(
"unchecked"
)
@Test
@Test
public
void
beansAreFound
()
{
public
void
beansAreFound
()
{
ApplicationContextRunner
contextRunner
=
new
ApplicationContextRunner
()
ApplicationContextRunner
contextRunner
=
new
ApplicationContextRunner
()
.
withUserConfiguration
(
EndpointConfiguration
.
class
);
.
withUserConfiguration
(
EndpointConfiguration
.
class
);
contextRunner
.
run
((
context
)
->
{
contextRunner
.
run
((
context
)
->
{
Map
<
String
,
Object
>
result
=
context
.
getBean
(
BeansEndpoint
.
class
).
beans
();
ApplicationContextDescriptor
result
=
context
.
getBean
(
BeansEndpoint
.
class
)
List
<
ApplicationContextDescriptor
>
contexts
=
(
List
<
ApplicationContextDescriptor
>)
result
.
beans
();
.
get
(
"contexts"
);
assertThat
(
result
.
getParent
()).
isNull
();
assertThat
(
contexts
).
hasSize
(
1
);
assertThat
(
result
.
getId
()).
isEqualTo
(
context
.
getId
());
ApplicationContextDescriptor
contextDescriptor
=
contexts
.
get
(
0
);
Map
<
String
,
BeanDescriptor
>
beans
=
result
.
getBeans
();
assertThat
(
contextDescriptor
.
getParentId
()).
isNull
();
assertThat
(
contextDescriptor
.
getId
()).
isEqualTo
(
context
.
getId
());
Map
<
String
,
BeanDescriptor
>
beans
=
contextDescriptor
.
getBeans
();
assertThat
(
beans
.
size
())
assertThat
(
beans
.
size
())
.
isLessThanOrEqualTo
(
context
.
getBeanDefinitionCount
());
.
isLessThanOrEqualTo
(
context
.
getBeanDefinitionCount
());
assertThat
(
contexts
.
get
(
0
).
getBeans
()
).
containsKey
(
"endpoint"
);
assertThat
(
beans
).
containsKey
(
"endpoint"
);
});
});
}
}
@SuppressWarnings
(
"unchecked"
)
@Test
@Test
public
void
infrastructureBeansAreOmitted
()
{
public
void
infrastructureBeansAreOmitted
()
{
ApplicationContextRunner
contextRunner
=
new
ApplicationContextRunner
()
ApplicationContextRunner
contextRunner
=
new
ApplicationContextRunner
()
...
@@ -75,32 +70,28 @@ public class BeansEndpointTests {
...
@@ -75,32 +70,28 @@ public class BeansEndpointTests {
.
filter
((
name
)
->
BeanDefinition
.
ROLE_INFRASTRUCTURE
==
factory
.
filter
((
name
)
->
BeanDefinition
.
ROLE_INFRASTRUCTURE
==
factory
.
getBeanDefinition
(
name
).
getRole
())
.
getBeanDefinition
(
name
).
getRole
())
.
collect
(
Collectors
.
toList
());
.
collect
(
Collectors
.
toList
());
Map
<
String
,
Object
>
result
=
context
.
getBean
(
BeansEndpoint
.
class
).
beans
();
ApplicationContextDescriptor
result
=
context
.
getBean
(
BeansEndpoint
.
class
)
List
<
ApplicationContextDescriptor
>
contexts
=
(
List
<
ApplicationContextDescriptor
>)
result
.
beans
();
.
get
(
"contexts"
);
Map
<
String
,
BeanDescriptor
>
beans
=
result
.
getBeans
();
Map
<
String
,
BeanDescriptor
>
beans
=
contexts
.
get
(
0
).
getBeans
();
for
(
String
infrastructureBean
:
infrastructureBeans
)
{
for
(
String
infrastructureBean
:
infrastructureBeans
)
{
assertThat
(
beans
).
doesNotContainKey
(
infrastructureBean
);
assertThat
(
beans
).
doesNotContainKey
(
infrastructureBean
);
}
}
});
});
}
}
@SuppressWarnings
(
"unchecked"
)
@Test
@Test
public
void
lazyBeansAreOmitted
()
{
public
void
lazyBeansAreOmitted
()
{
ApplicationContextRunner
contextRunner
=
new
ApplicationContextRunner
()
ApplicationContextRunner
contextRunner
=
new
ApplicationContextRunner
()
.
withUserConfiguration
(
EndpointConfiguration
.
class
,
.
withUserConfiguration
(
EndpointConfiguration
.
class
,
LazyBeanConfiguration
.
class
);
LazyBeanConfiguration
.
class
);
contextRunner
.
run
((
context
)
->
{
contextRunner
.
run
((
context
)
->
{
Map
<
String
,
Object
>
result
=
context
.
getBean
(
BeansEndpoint
.
class
).
beans
();
ApplicationContextDescriptor
result
=
context
.
getBean
(
BeansEndpoint
.
class
)
List
<
ApplicationContextDescriptor
>
contexts
=
(
List
<
ApplicationContextDescriptor
>)
result
.
beans
();
.
get
(
"contexts"
);
assertThat
(
context
).
hasBean
(
"lazyBean"
);
assertThat
(
context
).
hasBean
(
"lazyBean"
);
assertThat
(
contexts
.
get
(
0
)
.
getBeans
()).
doesNotContainKey
(
"lazyBean"
);
assertThat
(
result
.
getBeans
()).
doesNotContainKey
(
"lazyBean"
);
});
});
}
}
@SuppressWarnings
(
"unchecked"
)
@Test
@Test
public
void
beansInParentContextAreFound
()
{
public
void
beansInParentContextAreFound
()
{
ApplicationContextRunner
parentRunner
=
new
ApplicationContextRunner
()
ApplicationContextRunner
parentRunner
=
new
ApplicationContextRunner
()
...
@@ -110,31 +101,9 @@ public class BeansEndpointTests {
...
@@ -110,31 +101,9 @@ public class BeansEndpointTests {
.
withUserConfiguration
(
EndpointConfiguration
.
class
).
withParent
(
parent
)
.
withUserConfiguration
(
EndpointConfiguration
.
class
).
withParent
(
parent
)
.
run
(
child
->
{
.
run
(
child
->
{
BeansEndpoint
endpoint
=
child
.
getBean
(
BeansEndpoint
.
class
);
BeansEndpoint
endpoint
=
child
.
getBean
(
BeansEndpoint
.
class
);
Map
<
String
,
Object
>
result
=
endpoint
.
beans
();
ApplicationContextDescriptor
result
=
endpoint
.
beans
();
List
<
ApplicationContextDescriptor
>
contexts
=
(
List
<
ApplicationContextDescriptor
>)
result
assertThat
(
result
.
getParent
().
getBeans
()).
containsKey
(
"bean"
);
.
get
(
"contexts"
);
assertThat
(
result
.
getBeans
()).
containsKey
(
"endpoint"
);
assertThat
(
contexts
).
hasSize
(
2
);
assertThat
(
contexts
.
get
(
1
).
getBeans
()).
containsKey
(
"bean"
);
assertThat
(
contexts
.
get
(
0
).
getBeans
()).
containsKey
(
"endpoint"
);
});
});
}
@SuppressWarnings
(
"unchecked"
)
@Test
public
void
beansInChildContextAreNotFound
()
{
ApplicationContextRunner
parentRunner
=
new
ApplicationContextRunner
()
.
withUserConfiguration
(
EndpointConfiguration
.
class
);
parentRunner
.
run
((
parent
)
->
{
new
ApplicationContextRunner
().
withUserConfiguration
(
BeanConfiguration
.
class
)
.
withParent
(
parent
).
run
(
child
->
{
BeansEndpoint
endpoint
=
child
.
getBean
(
BeansEndpoint
.
class
);
Map
<
String
,
Object
>
result
=
endpoint
.
beans
();
List
<
ApplicationContextDescriptor
>
contexts
=
(
List
<
ApplicationContextDescriptor
>)
result
.
get
(
"contexts"
);
assertThat
(
contexts
).
hasSize
(
1
);
assertThat
(
contexts
.
get
(
0
).
getBeans
()).
containsKey
(
"endpoint"
);
assertThat
(
contexts
.
get
(
0
).
getBeans
()).
doesNotContainKey
(
"bean"
);
});
});
});
});
}
}
...
...
spring-boot-samples/spring-boot-sample-actuator/src/test/java/sample/actuator/SampleActuatorApplicationTests.java
View file @
a6b30a3a
...
@@ -217,10 +217,8 @@ public class SampleActuatorApplicationTests {
...
@@ -217,10 +217,8 @@ public class SampleActuatorApplicationTests {
.
withBasicAuth
(
"user"
,
getPassword
())
.
withBasicAuth
(
"user"
,
getPassword
())
.
getForEntity
(
"/application/beans"
,
Map
.
class
);
.
getForEntity
(
"/application/beans"
,
Map
.
class
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getStatusCode
()).
isEqualTo
(
HttpStatus
.
OK
);
assertThat
(
entity
.
getBody
()).
hasSize
(
1
);
assertThat
(
entity
.
getBody
()).
containsOnlyKeys
(
"beans"
,
"parent"
,
"id"
);
Map
<
String
,
Object
>
body
=
(
Map
<
String
,
Object
>)
((
List
<?>)
entity
.
getBody
()
assertThat
(((
String
)
entity
.
getBody
().
get
(
"id"
))).
startsWith
(
"application"
);
.
get
(
"contexts"
)).
get
(
0
);
assertThat
(((
String
)
body
.
get
(
"id"
))).
startsWith
(
"application"
);
}
}
@SuppressWarnings
(
"unchecked"
)
@SuppressWarnings
(
"unchecked"
)
...
...
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