import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
public class SpringVisualizer implements ApplicationContextAware {
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ConfigurableListableBeanFactory factory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory();
StringBuilder dotOutput = new StringBuilder();
dotOutput.append("digraph SpringBeans {\n");
dotOutput.append(" node [shape=box, fontname=\"Arial\"];\n");
for (String beanName : factory.getBeanDefinitionNames()) {
BeanDefinition def = factory.getBeanDefinition(beanName);
// Get dependencies (beans this bean depends on)
String[] dependencies = factory.getDependenciesForBean(beanName);
for (String dep : dependencies) {
// Format: "SourceBean" -> "TargetBean"
dotOutput.append(String.format(" \"%s\" -> \"%s\";\n", beanName, dep));
}
}
dotOutput.append("}");
System.out.println("----- COPY THE DOT OUTPUT BELOW -----");
System.out.println(dotOutput.toString());
System.out.println("-------------------------------------");
}
}
==========
<bean class="com.yourpackage.SpringVisualizer" />
Go to https://edotor.net/
==============
org.springframework.beans.factory.config.ConfigurableListableBeanFactory factory =
((org.springframework.context.ConfigurableApplicationContext) context).getBeanFactory();
StringBuilder dot = new StringBuilder("digraph SpringBeans {\n");
for (String name : factory.getBeanDefinitionNames()) {
for (String dep : factory.getDependenciesForBean(name)) {
dot.append(" \"").append(name).append("\" -> \"").append(dep).append("\";\n");
}
}
dot.append("}");
dot.toString(); // This will be the result shown in the IntelliJ window
==========
If you don't have the Context in your local variables:
In many frameworks like Quantum, the context is stored in a static "Holder" or "Registry." Try searching for a static getter in the Evaluate window first:
QuantumContext.getInstance().getSpringContext()
WebApplicationContextUtils.getWebApplicationContext(servletContext)
org.springframework.web.context.ContextLoader.getCurrentWebApplicationContext();
org.springframework.context.support.AbstractApplicationContext.class.getClassLoader()
.loadClass("org.springframework.context.ApplicationContext")
==============
package com.yourpackage;
import org.springframework.context.*;
public class ctx implements ApplicationContextAware {
public static ApplicationContext a; // Short name for easy typing in debugger
public void setApplicationContext(ApplicationContext c) { a = c; }
}
In the Debugger: You can now simply type com.yourpackage.ctx.a to access the entire bean factory.
XML entry: <bean class="com.yourpackage.ctx" />
=========
StringBuilder s = new StringBuilder("digraph G {");
String[] beans = ((org.springframework.context.ConfigurableApplicationContext)myContext).getBeanFactory().getBeanDefinitionNames();
for (String b : beans) {
for (String d : ((org.springframework.context.ConfigurableApplicationContext)myContext).getBeanFactory().getDependenciesForBean(b)) {
s.append("\"").append(b).append("\"->\"").append(d).append("\";");
}
}
s.append("}").toString();
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)