javascript
Spring5参考指南:AspectJ高级编程之Configurable
文章目錄
- 遇到的問題
- @Configurable
- 原理
- 重要配置
遇到的問題
前面的文章我們講到了在Spring中使用Aspect。但是Aspect的都是Spring管理的Bean。 現(xiàn)在有一個問題,實際工作中,我們經(jīng)常會想new一個Bean,然后在這個Bean中注入Spring管理的其他Bean。但是new出來的bean已經(jīng)脫離Spring的管控了。
該怎么處理呢?
@Configurable
Spring提供了一個@Configurable的注解,可以實現(xiàn)這個功能,我們看一個例子:
@Configurable(autowire= Autowire.BY_NAME, preConstruction = true) public class Account {private static Logger log= LoggerFactory.getLogger(Account.class);private String name;@Autowiredprivate BeanA beanA;public Account(){log.info("init Account");}public Object getBeanA() {return beanA;}public String getName() {return name;}public void setName(String name) {this.name = name;} }這里定義了一個Account類,它里面有依賴的BeanA,我們想在new Account()的時候, 直接使用Spring注入的BeanA。
preConstruction = true 表示依賴的Bean在構(gòu)造函數(shù)調(diào)用之前就被注入了。
autowire= Autowire.BY_NAME 表示依賴的Bean是按名字來自動裝配。當然也可以使用autowire= Autowire.BY_TYPE,按類型來裝配。
同時我們需要開啟SpringConfig支持:
@Configuration @EnableSpringConfigured public class AppConfig { }最后看下我們怎么調(diào)用:
public class ConfigurableApp {private static Logger log= LoggerFactory.getLogger(ConfigurableApp.class);public static void main(String[] args) {ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean-config.xml");Account accountA=new Account();log.info(accountA.getBeanA().toString());} }輸出結(jié)果如下:
07:37:27.917 [main] INFO com.flydean.beans.Account - init Account 07:37:27.917 [main] INFO com.flydean.ConfigurableApp - com.flydean.beans.BeanA@54c5a2ff可以看到雖然Account是new出來的,但是BeanA依然被注入到實例中。
原理
單獨使用@Configurable沒有任何作用。
Spring-Aspects.jar中的AnnotationBeanConfigurerAspect,才是讓@Configurable起作用的根本。本質(zhì)上,aspect是,“從用@Configurable注解的類型的新對象的初始化返回后,根據(jù)注解的屬性使用spring配置新創(chuàng)建的對象”。在此上下文中,“初始化”是指新實例化的對象(例如,用new運算符實例化的對象)以及正在進行反序列化的可序列化對象(例如,通過 readResolve())。
重要配置
下面是最最重要的pom配置了,這里我使用了aspectj-maven-plugin 這個插件來對spring-aspects.jar進行編織。 如下所示:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>spring5-core-workshop</artifactId><groupId>com.flydean</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>aop-advanced</artifactId><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>5.1.3.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>5.1.3.RELEASE</version></dependency></dependencies><build><!-- To define the plugin version in your parent POM --><pluginManagement><plugins><plugin><groupId>org.codehaus.mojo</groupId><artifactId>aspectj-maven-plugin</artifactId><version>1.11</version></plugin></plugins></pluginManagement><!-- To use the plugin goals in your POM or parent POM --><plugins><plugin><groupId>org.codehaus.mojo</groupId><artifactId>aspectj-maven-plugin</artifactId><configuration><complianceLevel>1.8</complianceLevel><source>1.8</source><target>1.8</target><outxml>true</outxml><verbose>true</verbose><showWeaveInfo>true</showWeaveInfo><aspectLibraries><aspectLibrary><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId></aspectLibrary></aspectLibraries></configuration><executions><execution><goals><goal>compile</goal> <!-- use this goal to weave all your main classes --><goal>test-compile</goal> <!-- use this goal to weave all your test classes --></goals></execution></executions></plugin></plugins></build></project>本文的例子可以參考aop-advanced
更多精彩內(nèi)容且看:
- 區(qū)塊鏈從入門到放棄系列教程-涵蓋密碼學(xué),超級賬本,以太坊,Libra,比特幣等持續(xù)更新
- Spring Boot 2.X系列教程:七天從無到有掌握Spring Boot-持續(xù)更新
- Spring 5.X系列教程:滿足你對Spring5的一切想象-持續(xù)更新
- java程序員從小工到專家成神之路(2020版)-持續(xù)更新中,附詳細文章教程
更多教程請參考 flydean的博客
總結(jié)
以上是生活随笔為你收集整理的Spring5参考指南:AspectJ高级编程之Configurable的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring5参考指南:AOP代理
- 下一篇: 分组密码与模式