您好,登錄后才能下訂單哦!
這篇文章主要介紹了Java中Spring框架之AOP如何配置,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
AOP(Aspect Oriented Programming)面向切面編程,是OOP(面向?qū)ο缶幊蹋┑闹匾a(bǔ)充,面向?qū)ο笫荍ava編程的基礎(chǔ),以封裝、繼承、多態(tài)建立了對象間的層次關(guān)系,關(guān)注的是每個對象的具體實現(xiàn)。面向?qū)ο笤试S我們開發(fā)縱向的關(guān)系。
AOP關(guān)注橫向關(guān)系,也就是說類和類之間不需要特定的關(guān)系,它能夠?qū)⒛承┩ㄓ玫牟僮鳎ㄈ纾喝罩咎幚?、安全驗證、事務(wù)處理、異常處理、性能統(tǒng)計等)插入到這些無關(guān)的類中,從而可以讓每個類只關(guān)注自己的核心邏輯,不必處理這些通用的操作,這個過程就是橫切,而這些通用的操作就是切面Aspect。
簡單來說:AOP能把和類的核心業(yè)務(wù)無關(guān),多個類又共同需要的邏輯代碼封裝起來,當(dāng)對象需要時自動調(diào)用,這樣就減少了類中的重復(fù)代碼,降低了類之間的耦合性,提高了程序的維護(hù)性。
1、橫切關(guān)注點
對哪些方法進(jìn)行攔截,攔截后怎么處理,這些關(guān)注點稱之為橫切關(guān)注點
2、切面(aspect)
類是對物體特征的抽象,切面就是對橫切關(guān)注點的抽象
3、連接點(joinpoint)
被攔截到的點,因為Spring只支持方法類型的連接點,所以在Spring中連接點指的就是被攔截到的方法,實際上連接點還可以是字段或者構(gòu)造器
4、切入點(pointcut)
對連接點進(jìn)行攔截的定義
5、通知(advice)
所謂通知指的就是指攔截到連接點之后要執(zhí)行的代碼,通知分為前置、后置、異常、最終、環(huán)繞通知五類
6、目標(biāo)對象
代理的目標(biāo)對象
7、織入(weave)
將切面應(yīng)用到目標(biāo)對象并導(dǎo)致代理對象創(chuàng)建的過程
8、引入(introduction)
在不修改代碼的前提下,引入可以在運(yùn)行期為類動態(tài)地添加一些方法或字段
1)日志 ,方法執(zhí)行開始和完成以及出現(xiàn)異常都可以用日志記錄
2)緩存 ,第一次調(diào)用查詢數(shù)據(jù)庫,將查詢結(jié)果放入內(nèi)存對象, 第二次調(diào)用, 直接從內(nèi)存對象返回,不需要查詢數(shù)據(jù)庫
3)事務(wù) ,調(diào)用方法前開啟事務(wù), 調(diào)用方法后提交關(guān)閉事務(wù)
等等
1)導(dǎo)入Maven依賴
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.14.Release</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
編寫Service接口和實現(xiàn)類,這里只完成模擬操作
public interface AdminService {
void saveAdmin();
void updateAdmin();
void deleteAdmin();
void selectAdmin();
}
public class AdminServiceImpl implements AdminService {
@Override
public void saveAdmin() {
System.out.println("Invoke saveAdmin");
}
@Override
public void updateAdmin() {
System.out.println("Invoke updateAdmin");
}
@Override
public void deleteAdmin() {
System.out.println("Invoke deleteAdmin");
}
@Override
public void selectAdmin() {
System.out.println("Invoke selectAdmin");
}
}
3)編寫自定義增強(qiáng)類,該類的方法對應(yīng)AOP的5種增強(qiáng)通知,分別是:
1)前置通知before :方法調(diào)用前執(zhí)行
2)后置通知after-returning:方法調(diào)用后執(zhí)行,出現(xiàn)異常不執(zhí)行
3)后置通知after:方法調(diào)用后執(zhí)行,出現(xiàn)異常也執(zhí)行
4)異常通知after-throwing:出現(xiàn)異常執(zhí)行
5)環(huán)繞通知around:方法調(diào)用前后執(zhí)行
/**
* 自定義增強(qiáng)類
*/
public class MyAdvise {
public void before() throws Throwable {
System.out.println("這是before");
}
public void afterReturning(){
System.out.println("這是afterReturning");
}
public void after(){
System.out.println("這是after");
}
public void afterThrowing() throws Throwable {
System.out.println("這是afterThrowing");
}
public Object around(ProceedingJoinPoint point) throws Throwable {
System.out.println("這是around -- 前置執(zhí)行");
Object obj = point.proceed();
System.out.println("這是around -- 后置執(zhí)行");
return obj;
}
}
4) Spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userService" class="com.qianfeng.springaop.UserServiceImpl"/>
<bean id="adminService" class="com.qianfeng.springaop.AdminServiceImpl"/>
<bean id="myAdvise" class="com.qianfeng.springaop.MyAdvise"/>
<aop:config>
<!--配置切入點
execution中第一個*代表任意返回值類型,后面是包名,第二個*代表類名的開頭,第三個*代表任意方法,(..)代表任意方法參數(shù)
-->
<aop:pointcut id="c" expression="execution(* com.qianfeng.springaop.*ServiceImpl.*(..))"/>
<!--配置方面-->
<aop:aspect ref="myAdvise">
<aop:before method="before" pointcut-ref="c"/>
<aop:after method="after" pointcut-ref="c"/>
<aop:after-returning method="afterReturning" pointcut-ref="c"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="c"/>
<aop:around method="around" pointcut-ref="c"/>
</aop:aspect>
</aop:config>
</beans>
5)運(yùn)行測試
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-aop.xml")
public class TestAOP {
@Test
public void test(){
ApplicationContext app = new
ClassPathXmlApplicationContext("applicationContext-aop.xml");
AdminService as = (AdminService) app.getBean("adminService");
as.deleteAdmin();
}
}
可以看到執(zhí)行任何Service類的方法,都會調(diào)用MyAdvise中的通知方法
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Java中Spring框架之AOP如何配置”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。