您可以使用ReaderEventListener
在上下文啓動時構建對象grahp。
例如:
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();
...
}
建立他們,你可以告訴更多一點關於你正試圖在這裏完成什麼? 「對象圖結構」是什麼意思? –
您可以看看[反射框架](http://docs.oracle.com/javase/tutorial/reflect/) –
Hi @ErikPragt,按對象結構我的意思是Spring對象創建的bean對象與其存在於內存中的依賴關係。在上面的例子中,存在於內存中的** College **對象的整個對象結構。 – Jaga