您可以ReaderEventListener
在上下文启动时使用 a 来构建对象图。
例如:
public class TrackingReaderEventListener extends EmptyReaderEventListener {
private Map<String, ComponentDefinition> components = new HashMap<String, ComponentDefinition>();
@Override
public void componentRegistered(ComponentDefinition componentDefinition) {
components.put(componentDefinition.getName(), componentDefinition);
}
public Map<String, ComponentDefinition> getComponentMap() {
return components;
}
public static void main (String[] args) {
TrackingReaderEventListener listener = new TrackingReaderEventListener();
ClassPathXmlApplicationContext context = new AppContext("applicationContext.xml", listener);
context.refresh(); // or may be load beans definitions only
}
}
class AppContext extends ClassPathXmlApplicationContext {
private ReaderEventListener readerListener;
public AppContext(String configLocation, ReaderEventListener readerListener) {
super(new String[] {configLocation} , false);
this.readerListener = readerListener;
}
@Override
protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
super.initBeanDefinitionReader(reader);
reader.setEventListener(readerListener);
}
}
或者直接从BeanFactory
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) context.getBeanFactory();
String[] beanNames = beanFactory.getBeanDefinitionNames();
for (String beanName : beanNames) {
BeanDefinition bd = beanFactory.getBeanDefinition(beanName);
PropertyValues pvs = bd.getPropertyValues();
String[] dependsOn = bd.getDependsOn();
...
}