AOP的第六種實(shí)現(xiàn)方式:自動代理(#千鋒#)

1.IUserService.java

package com.zy.aop06;

import java.util.List;

public interface IUserService {

? ? /**

? ? * 獲取所有的用戶對象列表

? ? * @return

? ? */

? ? List<Object> getAllUser();

? ? /**

? ? * 保存用戶

? ? * @param user

? ? * @return

? ? */

? ? boolean saveUser(Object user);

? ? /**

? ? * 根據(jù)用戶uid刪除該uid對應(yīng)的用戶信息

? ? * @param uid

? ? * @return

? ? */

? ? boolean deleteUser(int uid);

? ? /**

? ? * 更新指定用戶信息

? ? * @param obj

? ? * @return

? ? */

? ? boolean updateUser(Object obj);

? ? void getUserByUid();

}

2.UserServiceImpl.java

package com.zy.aop06;

import org.springframework.stereotype.Component;

import java.util.ArrayList;

import java.util.List;

@Component("us")

public class UserServiceImpl implements IUserService {

? ? @Override

? ? public List<Object> getAllUser() {

? ? ? ? System.out.println("--------getAllUser----------");

? ? ? ? return new ArrayList<>();

? ? }

? ? @Override

? ? public boolean saveUser(Object user) {

? ? ? ? System.out.println("--------saveUser----------");

? ? ? ? return true;

? ? }

? ? @Override

? ? public boolean deleteUser(int uid) {

? ? ? ? System.out.println("--------deleteUser----------");

? ? ? ? return false;

? ? }

? ? @Override

? ? public boolean updateUser(Object obj) {

? ? ? ? System.out.println("--------updateUser----------");

? ? ? ? return true;

? ? }

? ? @Override

? ? public void getUserByUid() {

? ? ? ? System.out.println("--------getUserByUid----------");

? ? ? ? System.out.println(1 / 0);

? ? ? ? String str = null;

? ? ? ? System.out.println(str.length());

? ? }

}

3.beans.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.xsd

? ? ? ? http://www.springframework.org/schema/aop

? ? ? ? http://www.springframework.org/schema/aop/spring-aop.xsd

? ? ? ? http://www.springframework.org/schema/context

? ? ? ? http://www.springframework.org/schema/context/spring-context.xsd">

? ? <!--

? ? context:component-scan 組件掃描

? ? ? ? base-package指定要掃描的包的路徑

? ? -->

? ? <context:component-scan base-package="com.zy.aop06" />

? ? <!--aop:aspectj-autoproxy標(biāo)簽實(shí)現(xiàn)自動代理-->

? ? <aop:aspectj-autoproxy />

</beans>

4.MyAspect.java

package com.zy.aop06;

import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.*;

import org.springframework.stereotype.Component;

/**

*? 以注解的方式實(shí)現(xiàn)的切面類MyAspect

*

*? ? ? 當(dāng)前類中的五種通知方式均以注解方式完成

*/

@Component? ? ? ? ? //? 標(biāo)注當(dāng)前類為一個組件

@Aspect? ? ? ? ? ? //? 標(biāo)注當(dāng)前類為一個切面類

public class MyAspect {

? ? /**

? ? * @Pointcut 注解為了避免相同的匹配規(guī)則被定義多處,專門定義該方法設(shè)置執(zhí)行的匹配規(guī)則,各個自行調(diào)用即可

? ? *? ? write once, only once

? ? */

? ? @Pointcut(value = "execution(* com.zy.aop06.*.*(..))")

? ? public void setAll(){}

? ? /**

? ? * @Before 表示該方法為一個前置通知

? ? * @param jp 連接點(diǎn)

? ? */

? ? @Before("setAll()")

? ? public void myBefore(JoinPoint jp){

? ? ? ? //System.out.println(jp.getArgs());

? ? ? ? System.out.println("this is before.");

? ? }


? ? /**

? ? * @After 表示該方法為一個后置通知

? ? * @param jp 連接點(diǎn)

? ? */

? ? @After("setAll()")

? ? public void myAfter(JoinPoint jp){

? ? ? ? //System.out.println(jp.getArgs());

? ? ? ? System.out.println("this is after.");

? ? }

? ? /**

? ? * @Around 表示該方法為一個環(huán)繞通知

? ? * @param pjp 處理連接點(diǎn)

? ? * @return 返回每個業(yè)務(wù)方法的返回值

? ? */

? ? @Around("setAll()")

? ? public Object myAround(ProceedingJoinPoint pjp){

? ? ? ? Object obj = null;

? ? ? ? try {

? ? ? ? ? ? System.out.println("this is around before");

? ? ? ? ? ? obj = pjp.proceed();

? ? ? ? ? ? System.out.println("this is around after");

? ? ? ? } catch (Throwable throwable) {

? ? ? ? ? ? throwable.printStackTrace();

? ? ? ? }

? ? ? ? return obj;

? ? }

? ? /**

? ? * @AfterReturning 表示該方法為一個帶有返回值的通知

? ? * @param jp 連接點(diǎn)

? ? * @param obj 業(yè)務(wù)方法的返回值

? ? */

? ? @AfterReturning(value = "setAll()", returning = "obj")

? ? public void myReturn(JoinPoint jp, Object obj){

? ? ? ? System.out.println("this is after returnning " + obj);

? ? }

? ? /**

? ? * @AfterThrowing 表示該方法為一個帶有異常的通知

? ? * @param jp 連接點(diǎn)

? ? * @param e Throwable對象

? ? */

? ? @AfterThrowing(value = "setAll()", throwing = "e")

? ? public void myThrowing(JoinPoint jp, Throwable e){

? ? ? ? System.out.println("this is after throwing " + e.getMessage());

? ? }

}

5.TestAOP06.java

package com.zy.aop06;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAOP06 {

? ? @Test

? ? public void testAOP06(){

? ? ? ? ApplicationContext ac = new ClassPathXmlApplicationContext("com/zy/aop06/beans.xml");

? ? ? ? IUserService us = ac.getBean("us", IUserService.class);

? ? ? ? us.getAllUser();

? ? ? ? us.deleteUser(1);

? ? ? ? us.saveUser("zhangsan");

? ? ? ? us.updateUser("lisi");

? ? ? ? us.getUserByUid();

? ? }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容