2017-06-05 87 views

回答

1

您可以使用BeanPostProcessor。它會前和初始化後通知任何豆:

public class PrintingBeanPostProcessor implements BeanPostProcessor { 

    @Override 
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { 
     System.out.println("Before init: " + beanName); 
     return bean; 
    } 

    @Override 
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { 
     System.out.println("After init: " + beanName); 
     return bean; 
    } 

} 

要註冊,請在我的配置類,我只是做:

@Bean 
    public BeanPostProcessor postProcessor() { 
     return new PrintingBeanPostProcessor(); 
    } 

這上啓動,然後打印:

Before init: resourceHandlerMapping 
After init: resourceHandlerMapping 
Before init: defaultServletHandlerMapping 
After init: defaultServletHandlerMapping 
Before init: mvcUriComponentsContributor 
After init: mvcUriComponentsContributor 
Before init: httpRequestHandlerAdapter 
After init: httpRequestHandlerAdapter 

(這顯然只是它打印的一小部分)

希望有幫助,

Artur

+0

這就是我想要的。謝謝! –