spring動態(tài)注冊bean?AOP失效原理解析
前言
本文的素材來自讀者的一個問題,他看過我之前寫的一篇博文聊聊如何把第三方服務(wù)注冊到我們項目的spring容器中。剛好他項目中也有類似這樣的一個需求,他就采用我文中介紹的第三種方法
調(diào)用beanFactory.registerSingleton()
一開始項目運行得還可以,后面他在這個第三方服務(wù)中使用AOP,發(fā)現(xiàn)AOP始終沒有生效。于是他就給我留言了。今天就來聊一下這個話題,為什么使用registerSingleton()注冊的bean,無法使AOP生效
問題根源
registerSingleton()這個方法直接將bean存放到單例池里面了。
如果對bean的生命周期有了解的朋友,應(yīng)該會知道,bean可能會經(jīng)過一系列的后置處理器后,再存放到單例池里面。因此這個bean可能是會被增強的,其中當然包括經(jīng)過AOP增強
而使用registerSingleton()相當于是直接走捷徑,不經(jīng)過后置處理器,一步到位直接存放到單例池中。如果第三方服務(wù)是直接通過new出來的,就是一個普通的對象,因此注入到IOC容器后,也只是一個普通的bean,并沒有任何增強
問題修復(fù)
方案一、不使用registerSingleton()
而是使用BeanDefinition注冊方式
這種方式本質(zhì)是讓這個對象完整經(jīng)歷了bean的生命周期
示例:
@Configuration public class HelloServiceConfiguration implements BeanFactoryPostProcessor { @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException { DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) configurableListableBeanFactory; BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(); AbstractBeanDefinition beanDefinition = beanDefinitionBuilder.getBeanDefinition(); beanDefinition.setBeanClass(HelloService.class); HelloServiceProperties properties = new HelloServiceProperties(); properties.setBeanName("helloService"); beanDefinition.getConstructorArgumentValues().addIndexedArgumentValue(0,properties); defaultListableBeanFactory.registerBeanDefinition(properties.getBeanName(),beanDefinition); } }
方案二、使用registerSingleton()
但注入的對象不是用new出來的,而是直接注入AOP代理對象
主要利用AOP的代理api:AnnotationAwareAspectJAutoProxyCreator
示例
@Configuration public class HelloServiceWithProxyConfiguration implements BeanFactoryAware, InitializingBean { private BeanFactory beanFactory; @Autowired private AnnotationAwareAspectJAutoProxyCreator annotationAwareAspectJAutoProxyCreator; @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { this.beanFactory = beanFactory; } @Override public void afterPropertiesSet() throws Exception { DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory)beanFactory; HelloServiceProperties properties = new HelloServiceProperties(); properties.setBeanName("helloService"); HelloService helloServicePrxoy = (HelloService) annotationAwareAspectJAutoProxyCreator.postProcessAfterInitialization(new HelloService(properties), "helloService$$Prxoy"); defaultListableBeanFactory.registerSingleton(properties.getBeanName(),helloServicePrxoy); } }
總結(jié)
以上兩種方案,建議使用方案一。因為方案一完整經(jīng)歷過bean的生命周期,這就意味著可以獲取spring提供的各種增強功能。方案二反而更像是硬編碼進去,如果后面要使用spring的其他增強的功能,就還必須調(diào)用其他API。不過如果可以確定業(yè)務(wù)不會使用spring提供的各種擴展功能。方案二也是可以的
以上就是spring動態(tài)注冊bean AOP失效原理解析的詳細內(nèi)容,更多關(guān)于spring AOP失效的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot集成WebSocket實現(xiàn)前后端消息互傳的方法
這篇文章主要介紹了SpringBoot集成WebSocket實現(xiàn)前后端消息互傳的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10SpringBoot使用WebJars統(tǒng)一管理靜態(tài)資源的方法
這篇文章主要介紹了SpringBoot使用WebJars統(tǒng)一管理靜態(tài)資源的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12Java使用FileInputStream流讀取文件示例詳解
這篇文章主要介紹了Java使用FileInputStream流讀取文件示例詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07