- 什么是面向方面的編程,為什么我們需要它
- 什么是AspectJ
- 將AspectJ與Spring結合使用(配置AspectJ和spring一起工作)
- 我將解釋以前帖子中示例的各個方面。
什么是面向方面的編程以及我們為什么需要它
在軟件開發過程中,我們可以使用不同的編程范例,例如OOP(面向對象編程)或POP(面向過程編程)。
今天,我們大多數人在軟件開發過程中使用面向對象的編程方法來解決現實生活中的問題。
但是在我們的工作中,我們經常遇到一些代碼,這些代碼貫穿我們的代碼庫,破壞了其模塊性并使它變得骯臟。
這部分代碼通常沒有業務價值,但是我們需要它們來解決問題。
例如,我們可以看一下數據庫事務。
事務對于我們的軟件非常重要,因為它們要注意數據的一致性。 啟動和處理事務的代碼對于我們的應用程序非常重要,但是它用于技術性工作(啟動,提交和回滾事務)。 這些東西使得很難理解代碼的真正含義(查看代碼的實際業務價值)。
當然,我不會在如何使用方面處理事務方面做任何示例,因為有很多框架會代替我們來處理事務。 我剛剛提到了事務,因為您可能知道如何使用簡單的JDBC API將數據插入數據庫。
因此,為了使代碼更整潔,我們將使用設計模式,這是解決問題的好方法。 但是有時候使用設計模式也不會導致我們找到簡單的解決方案,而我們大多數人會訴諸于更簡單的解決方案,從而產生“骯臟”的代碼。
在這種情況下,我們應該有機會采用面向方面的方法來解決問題。 在考慮AOP時,我們不應該考慮對我們來說是全新的東西,而應該將AOP視為對OOP的補充。 AOP可以簡化代碼模塊化,使代碼更整潔,并使我們更容易,更快地了解應用程序的某些部分應該做什么。
AOP引入了一些新概念,這將使我們更容易進行代碼調制。 如果我們想有效地使用Aspects,我們需要了解其基本原理和術語。
當我們開始使用AOP時,我們將遇到一個新的終端:
- 橫切關注點是應該在單獨的模塊中移動的代碼(即用于處理交易的代碼)。
- 方面,它是包含關注點的模塊。
- 切入點,我們可以將其視為指針,它將指示何時應運行相應的代碼
- 建議,它包含一個代碼,當到達某個連接點時應運行該代碼。
- 內部類型聲明,允許修改類結構。
- 方面編織是協調與系統其余部分集成的機制。
我將在示例中最后說明它們是什么以及如何使用它們。
什么是AspectJ
AspectJ是Java編程語言的擴展,它允許在Java編程語言中使用AOP概念。
使用AspectJ時,無需在現有代碼中進行任何更改。
AspectJ使用稱為Aspect的新結構擴展了Java,在AspectJ 5之后,您可以使用基于注釋的開發樣式。
AspectJ和Spring
Spring框架已經提供了自己的AOP實現。 Spring AOP是比AspectJ更簡單的解決方案,但它不如AspectJ強大。 因此,如果要在Spring應用程序中使用方面,在選擇AspectJ進行工作之前,應該熟悉Spring AOP的可能性。
在我們看到使用Aspect的示例之前,我將向您展示如何將AspectJ與Spring集成以及如何配置Tomcat以使其能夠在Spring中運行AspectJ應用程序。 在此示例中,我使用了方面的LTW(加載時間編織)。
因此,我將首先從Spring開始解釋如何做。 很簡單,只需在應用程序配置文件中添加下一行:
<context:load-time-weaver aspectj-weaving="autodetect"/>
這就是彈簧配置所要做的全部工作。
下一步是Tomcat的配置。 我們需要為應用程序定義新的類加載器。 該類加載器需要能夠進行加載時編織,因此我們使用:
<loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader" />
加載程序必須在Tomcat類路徑中,然后才能使用。
當然,為了使此工作有效,我們需要創建aop.xml文件。 該文件包含將在類轉換過程中由類加載器使用的指令。 這是我用于字母轉換的aop.xml文件的示例。
<aspectj><weaver options="-Xset:weaveJavaxPackages=true"><!-- only weave classes in our application-specific packages --><include within="ba.codecentric.medica.model.*" /><include within="ba.codecentric.medica..*.service.*" /><include within="ba.codecentric.medica.controller..*" /><include within="ba.codecentric.medica.utils.ModelMapper" /><include within="ba.codecentric.medica.utils.RedirectHelper" /><include within="ba.codecentric.medica.aop.aspect.CharacterConvertionAspect" /><include within="ba.codecentric.medica.security.UserAuthenticationProvider" /><include within="ba.codecentric.medica.wraper.MedicaRequestWrapper"/></weaver><aspects><!-- weave in just this aspect --><aspect name="ba.codecentric.medica.aop.aspect.CharacterConversionAspect" /></aspects>
</aspectj>
對于愿意嘗試AspectJ的所有人來說,最后一個xml文件最為有趣。 它指示AspectJ編織過程。
編織器部分包含有關應編織的信息。 因此,此文件將在其中包含所有類:
- ba.codecentric.medica.model。*
- ba.codecentric.medica .. *。service。*
- ba.codecentric.medica.controller .. *
- ba.codecentric.medica.utils.ModelMapper
- ba.codecentric.medica.utils.RedirectHelper
- ba.codecentric.medica.aop.aspect.CharacterConvertionAspect
- ba.codecentric.medica.security.UserAuthenticationProvider
- ba.codecentric.medica.wraper.MedicaRequestWrapper
因此,第一行包括模型包中的所有類。 第二個類包括所有類,它們是ba.codecentric.medica包(即ba.codecentric.medica.hospitalisation.service)內的service子包的一部分。 第三個包括控制器組件下方的所有內容。 其余各行包括指定的類。
選項屬性定義在編織過程中應使用的附加選項。 因此,在此示例中-Xset:weaveJavaxPackages = true指示AspectJ也編織Java包。 方面部分包含將在編織過程中使用的方面的列表。 有關使用xml配置的更多信息,請參見AspectJ文檔 。
使用示例AspectJ
我更喜歡使用注釋,因此下一個示例將向您展示如何將AspectJ與注釋一起使用。 從AspectJ 5版本開始,可以使用AspectJ進行注釋驅動的編程。這是一些完整方面的代碼,其中包含用于字母轉換的注意事項。
package ba.codecentric.medica.aop.aspect;import java.util.List;
import java.util.Map;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;import ba.codecentric.medica.utils.CharacterConverter;
import ba.codecentric.medica.utils.ContextHelper;
import ba.codecentric.medica.utils.LanguageHelper;/*** Aspect used for transformation characters from one alphabet to another. * * @author igor**/
@Aspect
public class CharacterConvertionAspect {private static Log LOG = LogFactory.getLog(CharacterConvertionAspect.class);public int getConvertTo() {return getLanguageHelper().getConvertTo();}protected LanguageHelper getLanguageHelper() {return ContextHelper.getBean("languageHelper");}public CharacterConvertionAspect() {LOG.info("Character converter aspect created");}@SuppressWarnings("rawtypes")@Around("execution(public java.lang.String ba.codecentric.medica.model..*.get*(..)) && !cflow(execution(* ba.codecentric.medica.controller..*.*(..))) && !cflow(execution(public void ba.codecentric.medica..*.service..*.*(..))) && !cflow(execution(* ba.codecentric.medica.security.UserAuthenticationProvider.*(..)))")public Object convertCharacters(ProceedingJoinPoint pjp) throws Throwable {LOG.info("Character conversion trigered");Object value = pjp.proceed();if (value instanceof String) {LOG.info("Convert:" + value);Signature signature = pjp.getSignature();Class type = signature.getDeclaringType();String methodName = signature.getName();Map<Class, List<string&lgt;&lgt; skipConvertionMap = getBlackList();if(skipConvertionMap.containsKey(type)){List<string&lgt; list = skipConvertionMap.get(type);if(list == null || list.contains(methodName)){LOG.info("Value will not be converted because it is on blacklist");return value;}}return getConverter().convertCharacters((String) value, getConvertTo());}LOG.info("Convertion will not be performed (" + value + ")");return value;}@Around("execution(public void ba.codecentric.medica.model..*.set*(java.lang.String))")public Object convertCharactersToLat(ProceedingJoinPoint pjp) throws Throwable {Object value = pjp.getArgs()[0];LOG.info("Converting value:" + value + ", before persisting");if (value instanceof String){value= getConverter().convertCharacters((String)value, CharacterConverter.TO_LAT);}return pjp.proceed(new Object[]{value});}/*** Convert parameter to Latin alphabet* * @param pjp* @return* @throws Throwable*/@Around("execution(public * ba.codecentric.medica.wraper.MedicaRequestWrapper.getParameter*(..))")public Object convertParametersToLat(ProceedingJoinPoint pjp) throws Throwable {Object value = pjp.proceed();return getConverter().convert(value, CharacterConverter.TO_LAT);}/*** If result of the invocation is String, it should be converted to chosen alphabet.* * @param jp* @return converted value* @throws Throwable*/@Around("execution(* ba.codecentric.medica.controller..*.*(..))")public Object procedWithControllerInvocation(ProceedingJoinPoint jp) throws Throwable {Object value = jp.proceed();return getConverter().convert(value, getConvertTo());}public CharacterConverter getConverter() {return ContextHelper.getBean("characterConverter");}@SuppressWarnings("rawtypes")public Map<Class,List<string&lgt;&lgt; getBlackList(){return ContextHelper.getBean("blackList");}}
首先,我們可以看到該類使用@Aspect注釋進行了注釋。 這表明此類實際上是一個方面。 Aspect是一種包含類似橫切關注點的構造。 因此,我們可以將其視為包含交叉代碼的模塊,并定義何時使用代碼以及如何使用。
@Around("execution(public void ba.codecentric.medica.model..*.set*(java.lang.String))")
public Object convertCharactersToLat(ProceedingJoinPoint pjp) throws Throwable {Object value = pjp.getArgs()[0];LOG.debug("Converting value:" + value + ", before persisting");if (value instanceof String) {value = getConverter().convertCharacters((String) value, CharacterConverter.TO_LAT);}return pjp.proceed(new Object[] { value });
}
這是一種使用@Around注釋進行注釋的方法。 周圍注釋用于表示周圍建議。 我已經提到過,建議是包含跨領域代碼的地方。 在此示例中,我僅使用了“周圍”建議,但是除了在返回之前,之后,之后和引發建議之后還有其他建議。 除了周圍的所有建議,都不應有返回值。 注釋周圍的內容定義了何時編織建議中的代碼。 當我們定義切入點時,也可以做到這一點。 在此示例中,我沒有使用切入點來定義連接點,因為它是簡單的方面。 使用切入點注釋,您可以定義真正的魯棒連接點。 在這種情況下,將在設置只有一個類型為String的參數的實體bean的值期間執行建議。
在上面的示例中,ProcidingJoinPoint pjp提供了連接點,因此在此示例中,它是實體bean的setter方法。 發送到實體設置器方法的對象的值將首先轉換,然后將使用轉換后的值調用設置器方法。 如果我不使用方面,我的代碼可能如下所示:
public void setJmbg(String jmbg) {this.jmbg = getConverter().convertCharacters(jmbg, CharacterConverter.TO_LAT);
}
我已經說過,在此示例中,我使用LTW。 因此,在接下來的幾行中,我將嘗試簡要地解釋編織過程。 編織是其中類以定義的方面進行轉換的過程。 在下一張圖片中,您可以看到編織過程的圖示。

為了更好地理解編織,在這種情況下,可以將其視為圍繞調用方法的代碼注入。
結論
因此,在此示例中,我僅介紹了使用AspectJ進行方面編程的一些基本原理。 這方面幫助我保持了代碼的整潔。 使用方面的結果是跨界代碼與實際業務價值代碼的清晰分離。 控制器,服務和實體Bean保持整潔,并且將技術代碼提取到單獨的模塊中,使您可以更輕松地理解和維護代碼。 有關定義切入點和AspectJ項目常規的更多詳細信息,您可以在項目頁面上看到。
祝您編程愉快,別忘了分享!
參考:在Igor Madjeric博客上,我們的JCG合作伙伴 Igor Madjeric 提供了一些方面干凈的代碼 。
翻譯自: https://www.javacodegeeks.com/2012/10/clean-code-with-aspects.html