LiveBeansView produces a well-defined (and valid) JSON document now
Issue: SPR-9662
This commit is contained in:
parent
7b30ffd522
commit
3c557bfbc3
|
|
@ -18,6 +18,7 @@ package org.springframework.context.support;
|
||||||
|
|
||||||
import java.lang.management.ManagementFactory;
|
import java.lang.management.ManagementFactory;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import javax.management.MBeanServer;
|
import javax.management.MBeanServer;
|
||||||
|
|
@ -116,40 +117,50 @@ public class LiveBeansView implements LiveBeansViewMBean, ApplicationContextAwar
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Actually generate a JSON snapshot of the beans in the given ApplicationContexts
|
* Actually generate a JSON snapshot of the beans in the given ApplicationContexts.
|
||||||
|
* <p>This implementation doesn't use any JSON parsing libraries in order to avoid
|
||||||
|
* third-party library dependencies. It produces an array of context description
|
||||||
|
* objects, each containing a context and parent attribute as well as a beans
|
||||||
|
* attribute with nested bean description objects. Each bean object contains a
|
||||||
|
* bean, scope, type and resource attribute, as well as a dependencies attribute
|
||||||
|
* with a nested array of bean names that the present bean depends on.
|
||||||
* @param contexts the set of ApplicationContexts
|
* @param contexts the set of ApplicationContexts
|
||||||
* @return the JSON document
|
* @return the JSON document
|
||||||
*/
|
*/
|
||||||
protected String generateJson(Set<ConfigurableApplicationContext> contexts) {
|
protected String generateJson(Set<ConfigurableApplicationContext> contexts) {
|
||||||
StringBuilder result = new StringBuilder();
|
StringBuilder result = new StringBuilder("[\n");
|
||||||
for (ConfigurableApplicationContext context : contexts) {
|
for (Iterator<ConfigurableApplicationContext> it = contexts.iterator(); it.hasNext();) {
|
||||||
result.append("{\n\"context\": \"").append(context.getId()).append("\"\n");
|
ConfigurableApplicationContext context = it.next();
|
||||||
|
result.append("{\n\"context\": \"").append(context.getId()).append("\",\n");
|
||||||
if (context.getParent() != null) {
|
if (context.getParent() != null) {
|
||||||
result.append("\"parent\": \"").append(context.getParent().getId()).append("\"\n");
|
result.append("\"parent\": \"").append(context.getParent().getId()).append("\",\n");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
result.append("\"parent\": null\n");
|
result.append("\"parent\": null,\n");
|
||||||
}
|
}
|
||||||
|
result.append("\"beans\": [\n");
|
||||||
ConfigurableListableBeanFactory bf = context.getBeanFactory();
|
ConfigurableListableBeanFactory bf = context.getBeanFactory();
|
||||||
String[] beanNames = bf.getBeanDefinitionNames();
|
String[] beanNames = bf.getBeanDefinitionNames();
|
||||||
for (String beanName : beanNames) {
|
for (int i = 0; i < beanNames.length; i++) {
|
||||||
|
String beanName = beanNames[i];
|
||||||
BeanDefinition bd = bf.getBeanDefinition(beanName);
|
BeanDefinition bd = bf.getBeanDefinition(beanName);
|
||||||
if (bd.getRole() != BeanDefinition.ROLE_INFRASTRUCTURE &&
|
if (bd.getRole() != BeanDefinition.ROLE_INFRASTRUCTURE &&
|
||||||
(!bd.isLazyInit() || bf.containsSingleton(beanName))) {
|
(!bd.isLazyInit() || bf.containsSingleton(beanName))) {
|
||||||
result.append("{\n\"bean\": \"").append(beanName).append("\"\n");
|
result.append("{\n\"bean\": \"").append(beanName).append("\",\n");
|
||||||
String scope = bd.getScope();
|
String scope = bd.getScope();
|
||||||
if (!StringUtils.hasText(scope)) {
|
if (!StringUtils.hasText(scope)) {
|
||||||
scope = BeanDefinition.SCOPE_SINGLETON;
|
scope = BeanDefinition.SCOPE_SINGLETON;
|
||||||
}
|
}
|
||||||
result.append("\"scope\": \"").append(scope).append("\"\n");
|
result.append("\"scope\": \"").append(scope).append("\",\n");
|
||||||
Class beanType = bf.getType(beanName);
|
Class beanType = bf.getType(beanName);
|
||||||
if (beanType != null) {
|
if (beanType != null) {
|
||||||
result.append("\"type\": \"").append(beanType.getName()).append("\"\n");
|
result.append("\"type\": \"").append(beanType.getName()).append("\",\n");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
result.append("\"type\": null\n");
|
result.append("\"type\": null,\n");
|
||||||
}
|
}
|
||||||
result.append("\"resource\": \"").append(bd.getResourceDescription()).append("\"\n");
|
String resource = StringUtils.replace(bd.getResourceDescription(), "\\", "/");
|
||||||
|
result.append("\"resource\": \"").append(resource).append("\",\n");
|
||||||
result.append("\"dependencies\": [");
|
result.append("\"dependencies\": [");
|
||||||
String[] dependencies = bf.getDependenciesForBean(beanName);
|
String[] dependencies = bf.getDependenciesForBean(beanName);
|
||||||
if (dependencies.length > 0) {
|
if (dependencies.length > 0) {
|
||||||
|
|
@ -159,11 +170,19 @@ public class LiveBeansView implements LiveBeansViewMBean, ApplicationContextAwar
|
||||||
if (dependencies.length > 0) {
|
if (dependencies.length > 0) {
|
||||||
result.append("\"");
|
result.append("\"");
|
||||||
}
|
}
|
||||||
result.append("]\n}\n");
|
result.append("]\n}");
|
||||||
|
if (i < beanNames.length - 1) {
|
||||||
|
result.append(",\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
result.append("]\n");
|
||||||
result.append("}");
|
result.append("}");
|
||||||
|
if (it.hasNext()) {
|
||||||
|
result.append(",\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
result.append("]");
|
||||||
return result.toString();
|
return result.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue