日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用注解方式进行aop编程(代码)

發布時間:2025/3/19 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用注解方式进行aop编程(代码) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

UserDao.java接口:

package com.liu.jdk;public interface UserDao {public void addUser();public void deleteUser();}

UserDaoImpl.java實現類/目標類:

package com.liu.jdk;import org.springframework.stereotype.Repository;//目標類 @Repository("userDao") public class UserDaoImpl implements UserDao {@Overridepublic void addUser() {// TODO Auto-generated method stubSystem.out.println("添加方法");int i = 1/0;}@Overridepublic void deleteUser() {// TODO Auto-generated method stubSystem.out.println("刪除方法");}}

MyAspect.java切面類:

package com.liu.aspect.annotation;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
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;
import org.springframework.stereotype.Component;

//切面類
@Aspect
@Component
public class MyAspect {
@Pointcut(“execution(* com.liu.jdk..(…))”)
//需要一個返回值為void 方法體為空的方法來命名切入點
private void myPointCut() {}

//前置通知 @Before("myPointCut()") public void before(JoinPoint joinPoint) {System.out.println("前置通知開始,模擬檢查權限 ");System.out.println("目標類:"+joinPoint.getTarget());System.out.println("目標方法為:"+joinPoint.getSignature().getName()); } //后置通知 @AfterReturning("myPointCut()") public void afterRunning(JoinPoint joinPoint) {System.out.println("后置通知開始,模擬記錄日志");System.out.println("目標方法為:"+joinPoint.getSignature().getName()); }//環繞通知 @Around("myPointCut()") public Object around(ProceedingJoinPoint proceedingJoinPoint)throws Throwable {System.out.println("環繞通知開始");Object obj = proceedingJoinPoint.proceed();System.out.println("環繞通知結束");return obj; }//異常通知 @AfterThrowing(value = "myPointCut()",throwing = "e") public void afterThrowing(JoinPoint joinPoint,Throwable e) {System.out.println("異常通知,原因在于:"+e.getMessage()); }//最終通知 @After("myPointCut()") public void after() {System.out.println("最終通知"); }

}

ApplicationContext.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.3.xsd "><!-- 指定需要掃描的包 開啟注解--><context:component-scan base-package="com.liu"></context:component-scan><!-- 啟動基于注解的聲明式AspectJ支持 --><aop:aspectj-autoproxy /> </beans>

Test測試類:

package com.liu.aspect.annotation;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;import com.liu.jdk.UserDao;public class Test {public static void main(String[] args) {String xmlpath = "com/liu/aspect/annotation/ApplicationContext.xml";ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlpath);UserDao userDao = (UserDao) applicationContext.getBean("userDao");userDao.addUser();}}

運行結果:

環繞通知開始 前置通知開始,模擬檢查權限 目標類:com.liu.jdk.UserDaoImpl@5158b42f 目標方法為:addUser 添加方法 最終通知 異常通知,原因在于:/ by zero

總結

以上是生活随笔為你收集整理的使用注解方式进行aop编程(代码)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。