package com.yourpackage;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import java.io.*;
import java.util.*;
public class SpringVisualizer implements ApplicationContextAware, InitializingBean {
private ConfigurableApplicationContext context;
private final Set<String> currentBeanManualDeps = new HashSet<>();
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.context = (ConfigurableApplicationContext) applicationContext;
}
@Override
public void afterPropertiesSet() throws Exception {
System.err.println("!!! FORCING GRAPH GENERATION !!!");
generateGraph();
}
public void generateGraph() {
StringBuilder dot = new StringBuilder("digraph G {\n");
// 'sfdp' or 'neato' engines handle large graphs better, but we stick to 'dot' with better spacing
dot.append(" rankdir=LR; nodesep=0.5; ranksep=2.0; overlap=false; splines=true;\n");
dot.append(" node [shape=plain, fontname=\"Verdana\", fontsize=10];\n");
dot.append(" edge [fontname=\"Verdana\", fontsize=8, color=\"#888888\"];\n\n");
Set<String> processed = new HashSet<>();
int count = 0;
ApplicationContext currentCtx = this.context;
while (currentCtx != null) {
if (currentCtx instanceof ConfigurableApplicationContext) {
ConfigurableListableBeanFactory factory = ((ConfigurableApplicationContext) currentCtx).getBeanFactory();
for (String name : factory.getBeanDefinitionNames()) {
// FILTER: Skip internal Spring noise to ensure it renders
if (name.startsWith("org.springframework") || name.contains("AutoProxyCreator")) continue;
if (processed.add(name)) {
try {
BeanDefinition def = factory.getBeanDefinition(name);
currentBeanManualDeps.clear();
count++;
String color = name.toLowerCase().contains("comet") ? "#FFF9C4" :
(name.toLowerCase().contains("cpls") ? "#C8E6C9" : "#E1F5FE");
dot.append(" \"").append(name).append("\" [label=<");
dot.append("<TABLE BORDER=\"1\" CELLBORDER=\"0\" CELLSPACING=\"0\" BGCOLOR=\"").append(color).append("\">");
dot.append("<TR><TD BORDER=\"1\" BGCOLOR=\"#999999\"><B>").append(safeXml(name)).append("</B></TD></TR>");
for (PropertyValue pv : def.getPropertyValues().getPropertyValues()) {
String val = extractValueFlattened(pv.getValue(), 0);
dot.append("<TR><TD ALIGN=\"LEFT\" BORDER=\"1\">").append(safeXml(pv.getName())).append(": ").append(val).append("</TD></TR>");
}
dot.append("</TABLE>>];\n");
// Connections
Set<String> allDeps = new HashSet<>(Arrays.asList(factory.getDependenciesForBean(name)));
allDeps.addAll(currentBeanManualDeps);
for (String dep : allDeps) {
if (processed.contains(dep) || !dep.startsWith("org.springframework")) {
dot.append(" \"").append(name).append("\" -> \"").append(dep).append("\";\n");
}
}
} catch (Exception e) { }
}
}
}
currentCtx = currentCtx.getParent();
}
dot.append("}\n");
save(dot.toString(), count);
}
// Flattened logic: No nested tables, just strings. This is MUCH easier for Edotor to draw.
private String extractValueFlattened(Object value, int depth) {
if (value == null) return "null";
if (depth > 3) return "[...]";
if (value instanceof BeanReference) {
String bName = ((BeanReference) value).getBeanName();
currentBeanManualDeps.add(bName);
return "@" + safeXml(bName);
}
if (value instanceof TypedStringValue) {
return safeXml(((TypedStringValue) value).getValue());
}
if (value instanceof BeanDefinition || value instanceof BeanDefinitionHolder) {
BeanDefinition inner = (value instanceof BeanDefinitionHolder) ?
((BeanDefinitionHolder) value).getBeanDefinition() : (BeanDefinition) value;
String cls = inner.getBeanClassName();
String shortCls = (cls != null) ? cls.substring(cls.lastIndexOf('.') + 1) : "InnerBean";
return "[Bean: " + safeXml(shortCls) + "]";
}
if (value instanceof Map) {
return "{Map: " + ((Map<?, ?>) value).size() + " entries}";
}
if (value instanceof Iterable) {
return "[List/Set]";
}
return safeXml(value.toString());
}
private void save(String data, int count) {
try {
File f = new File(findRoot(), "build/spring-beans.dot");
f.getParentFile().mkdirs();
try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)))) {
out.print(data);
}
System.err.println("SUCCESS: File saved with " + count + " beans.");
} catch (Exception e) { e.printStackTrace(); }
}
private File findRoot() {
File f = new File(System.getProperty("user.dir"));
while (f != null && !new File(f, "build.gradle").exists()) f = f.getParentFile();
return f != null ? f : new File(System.getProperty("user.dir"));
}
private String safeXml(String s) {
if (s == null) return "";
return s.trim().replace("&", "&").replace("<", "<").replace(">", ">").replace("\"", """);
}
}
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)