JAVA 架構-Spring-AOP面向切面

來源:互聯網
上載者:User

標籤:image   run   dao   處理   成員   資料訪問層   bubuko   wing   log   

AOP(Aspect Orient Programming),我們一般稱為面向方面(切面)編程,作為物件導向的一種補充,用於處理系統中分佈於各個模組的橫切關注點,比如交易管理、日誌、緩衝等等。AOP實現的關鍵在於AOP架構自動建立的AOP代理,AOP代理主要分為靜態代理和動態代理,靜態代理的代表為AspectJ;而動態代理則以Spring AOP為代表。

AOP配置樣本:

資料訪問層:

package com.hanqi.dao;public class AppuserDao {public int deleteUser(int ids) {System.out.println(ids +"被刪除");yichang();//拋出一個異常return 1;}public void yichang() {throw new RuntimeException("出現錯誤!");}}

 切面代理層:

package com.hanqi.util;public class LogginProxy {public void beforeMethod() {System.out.println("方法之前被調用!");}public void afterMethod() {System.out.println("方法之後被調用!");}public void returnMethod() {System.out.println("返回結果時被調用!");}public void throwMethod() {System.out.println("拋出異常時被調用!");}/*public void aroundMethod() {System.out.println("方法被調用");}*/}

 spring.xml配置AOP

?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"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"><bean id="logginProxy" class="com.hanqi.util.LogginProxy"></bean><bean id="appuserdao" class="com.hanqi.dao.AppuserDao"></bean><aop:config><!-- 配置切點 --><aop:pointcutexpression="execution(* com.hanqi.dao.*.*(..))" id="aoppointcut" /><!--execution:執行,這句意為在執行com.hanqi.dao下的所有類時都會執行切面類--><!-- 指明切面類 --><aop:aspect ref="logginProxy"><aop:before method="beforeMethod"  pointcut-ref="aoppointcut"/><aop:after method="afterMethod" pointcut-ref="aoppointcut"/><aop:after-returning method="returnMethod" pointcut-ref="aoppointcut"/><aop:after-throwing method="throwMethod" pointcut-ref="aoppointcut"/></aop:aspect></aop:config></beans>

 JUnit Test測試:

package com.hanqi.util;import static org.junit.jupiter.api.Assertions.*;import org.junit.jupiter.api.AfterEach;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.hanqi.dao.AppuserDao;class JUnittest {private ClassPathXmlApplicationContext c;private AppuserDao appuserdao;@BeforeEachvoid setUp() throws Exception {c = new ClassPathXmlApplicationContext("spring.xml");appuserdao = c.getBean(AppuserDao.class);}@AfterEachvoid tearDown() throws Exception {c.close();}@Testvoid test() {appuserdao.deleteUser(55);}}

 列印結果:

使用註解配置AOP

spring.xml配置:

<?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"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"><context:component-scan base-package="com.hanqi"></context:component-scan><!--配置掃描器--></beans>

資料訪問層:

package com.hanqi.dao;import org.springframework.stereotype.Repository;@Repository//加到spring容器中,id名預設為類名首字母小寫public class AppuserDao {public int deleteUser(int ids) {System.out.println(ids +"被刪除");yichang();return 1;}public void yichang() {throw new RuntimeException("出現錯誤!");}}

 AppuserService類:

package com.hanqi.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Repository;import com.hanqi.dao.AppuserDao;@Repositorypublic class AppuserService {@Autowired//將該類自動放到當前類中的成員變數中,預設的裝配類型時byType//@Qualifier("dao")該註解可將bean中id="dao"的類裝配到該成員變數中,用於區分當類型相同時;private AppuserDao appuserDao;public int deleteUser(int ids) {return appuserDao.deleteUser(ids);}}

 代理類:

package com.hanqi.util;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.springframework.context.annotation.EnableAspectJAutoProxy;import org.springframework.stereotype.Repository;@Repository//加到spring容器中,id名為類名首字母小寫@Aspect//聲明當前類為切面類@EnableAspectJAutoProxy//自動代理public class LogginProxy {@Before("execution(* com.hanqi.dao.*.*(..))")//聲明切點public void beforeMethod() {System.out.println("方法之前被調用!");}@After("execution(* com.hanqi.dao.*.*(..))")public void afterMethod() {System.out.println("方法之後被調用!");}@AfterReturning("execution(* com.hanqi.dao.*.*(..))")public void returnMethod() {System.out.println("返回結果時被調用!");}@AfterThrowing("execution(* com.hanqi.dao.*.*(..))")public void throwMethod() {System.out.println("拋出異常時被調用!");}/*public void aroundMethod() {System.out.println("方法被調用");}*/}

 測試類別:

package com.hanqi.util;import org.junit.jupiter.api.AfterEach;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.hanqi.dao.AppuserDao;import com.hanqi.service.AppuserService;class JUnittest {private ClassPathXmlApplicationContext c;private AppuserDao appuserdao;private AppuserService appuserService;@BeforeEachvoid setUp() throws Exception {c = new ClassPathXmlApplicationContext("spring.xml");//appuserdao = c.getBean(AppuserDao.class);appuserService = c.getBean(AppuserService.class);}@AfterEachvoid tearDown() throws Exception {c.close();}@Testvoid test() {//appuserdao.deleteUser(55);appuserService.deleteUser(66);}}

 

JAVA 架構-Spring-AOP面向切面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.