2012-05-16 64 views

回答

1

您可以創建一個ReaderEventListener並記錄它們。但你需要覆蓋initBeanDefinitionReader設置ListenerXmlBeanDefinitionReader(我認爲沒有其他的方式,但可能會......)。

例如:

public class LogReaderEventListener extends EmptyReaderEventListener { 

    private static final Log log = LogFactory.getLog(LogReaderEventListener.class); 

    @Override 
    public void componentRegistered(ComponentDefinition componentDefinition) { 

     log.info("Registered Component [" + componentDefinition.getName() + "]"); 
     for (BeanDefinition bd : componentDefinition.getBeanDefinitions()) { 
      String name = bd.getBeanClassName(); 

      if (bd instanceof BeanComponentDefinition) { 
       name = ((BeanComponentDefinition) bd).getBeanName(); 
      } 
      log.info("Registered bean definition: [" + name + "]" + 
        " from " + bd.getResourceDescription()); 
     } 
    } 


    public static void main (String[] args) { 
     ClassPathXmlApplicationContext context = new LogXmlContext("applicationContext.xml"); 
     context.close(); 

    } 

} 

class LogXmlContext extends ClassPathXmlApplicationContext { 

    public LogXmlContext(String configLocation) { 
     super(configLocation); 
    } 

    @Override 
    protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) { 
     super.initBeanDefinitionReader(reader); 
     reader.setEventListener(new LogReaderEventListener()); 
    } 

} 
+0

有趣....會檢查這種方法 –