標籤:
註解AOP實現 源碼結構:
1、首先我們建立一個介面,love 談戀愛介面。
package com.spring.aop;/** * 談戀愛介面 * * @author Administrator * */public interface Love{ /* * 談戀愛方法 */ void fallInLove(); void test() throws Exception;}
2、我們寫一個Person類實現Love介面
package com.spring.aop;/** * 人對象 * * @author [email protected] * */public class Person implements Love{ /* * 重寫談戀愛方法 */ @Override public void fallInLove() { System.out.println("談戀愛了..."); } @Override public void test() throws Exception { // TODO Auto-generated method stub throw new Exception("我就說你們在一起不會幸福的,你能拿我怎麼滴?"); }}3、下面我們來寫aop 註解通知類【執行方法前 執行方法後 執行方法前後也稱為環繞方法 方法執行過程中拋出異常】
package com.spring.aop;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;/** * 註解方式 aop通知類 * * @author [email protected] * */@Aspectpublic class LoveHelper{ @Pointcut("execution(* com.spring.aop.*..*(..))") private void loveMethod() { }// 定義一個切入點 // 在調用方法之前執行 執行攔截包com.spring.aop.*下所有的方法 @Before("execution(* com.spring.aop.*..*(..))") public void before(JoinPoint point) throws Throwable { System.out.println("before::method " + point.getTarget().getClass().getName() + "." + point.getSignature().getName()); System.out.println("談戀愛之前必須要彼此瞭解!"); } // 在調用方法前後執行 @Around("execution(* com.spring.aop.*..*(..))") public Object around(ProceedingJoinPoint point) throws Throwable { System.out.println("around::method " + point.getTarget().getClass().getName() + "." + point.getSignature().getName()); if (point.getArgs().length > 0) { return point.proceed(point.getArgs()); } else { return point.proceed(); } } // 在調用方法之後執行 @After("execution(* com.spring.aop.*..*(..))") public void afterReturning(JoinPoint point) throws Throwable { System.out.println("method " + point.getTarget().getClass().getName() + "." + point.getSignature().getName()); System.out.println("我們已經談了5年了,最終還是分手了!"); // System.out.println("我們已經談了5年了,最終步入了結婚的殿堂!"); } // 當拋出異常時被調用 @AfterThrowing(value = "execution(* com.spring.aop.*..*(..))", throwing = "ex") public void doThrowing(JoinPoint point, Throwable ex) { System.out.println("doThrowing::method " + point.getTarget().getClass().getName() + "." + point.getSignature().getName() + " throw exception"); System.out.println(ex.getMessage()); }}4、配置好application.xml 就配置好bean和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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- 配置bean --> <bean id="person" class="com.spring.aop.Person"> </bean> <!-- 配置通知方法類 --> <bean id="loveHelper" class="com.spring.aop.LoveHelper"> </bean> <aop:aspectj-autoproxy /></beans>
5、寫上我們的測試類別 測試下效果 嘿嘿~~~
package com.spring.aop;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.spring.aop.Love;public class LoveTest{ public static void main(String[] args) { ApplicationContext appCtx = new ClassPathXmlApplicationContext( "applicationContext.xml"); Love love = (Love) appCtx.getBean("person"); love.fallInLove(); try { // 測試異常捕獲 love.test(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }}6、或者寫上Junit測試類別
package com.love.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.spring.aop.Love;import junit.framework.TestCase;public class LoveAopTest extends TestCase{ protected void setUp() { /* 開始test前的準備操作:初始化,擷取資料連線... */ } protected void tearDown() { /* 完成test後的清理工作:關閉檔案, 關閉資料連線... */ } public void testCase2() { ApplicationContext appCtx = new ClassPathXmlApplicationContext( "applicationContext.xml"); Love love = (Love) appCtx.getBean("person"); love.fallInLove(); try { // 測試異常捕獲 love.test(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); assertTrue(true); } }}7、看控制台輸出結果 調用了我們定義的aop攔截方法~~~ ok了
8、源碼:源碼下載
Spring AOP實現方式三【附源碼】