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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

011_AOP注解开发

發(fā)布時間:2025/4/17 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 011_AOP注解开发 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一. Spring的基于ApsectJ的注解的AOP開發(fā)

1. 編寫目標類并配置

2. 編寫切面類并配置

3. 在配置文件中打開注解的AOP開發(fā)

4. 在切面類上使用注解@Aspect

5. 前置通知@Before

6. 后置通知@AfterReturning

7. 注解配置切入點

8. 環(huán)繞通知@Around, 使用切入點

9. 異常拋出通知@AfterThrowing

10. 最終通知@After

二. AOP注解開發(fā)例子

1. 新建一個名為AOPAnnotation的Java項目

2. 創(chuàng)建UserDaoImpl.java類

package com.lywgames.dao.impl;public class UserDaoImpl{public void insert() {System.out.println("插入數(shù)據(jù)");}public void select() {System.out.println("查詢數(shù)據(jù)");}public void update() {System.out.println("更新數(shù)據(jù)");throw new RuntimeException();}public int delete() {System.out.println("刪除數(shù)據(jù)");return 1;}}

3. 創(chuàng)建AspectJAop.java切面類

package com.lywgames.aop;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;/*** 切面類*/ @Aspect public class AspectJAop {@Before(value="execution(* com.lywgames.dao.impl.UserDaoImpl.insert(..))")public void beforeInsertCheck() {System.out.println("檢測插入數(shù)據(jù)");}@AfterReturning(value="execution(* com.lywgames.dao.impl.UserDaoImpl.delete(..))", returning="result")public void afterDelete(int result) {System.out.println("刪除后返回值:" + result);}@Around(value="AspectJAop.selectPoint()")public Object arround(ProceedingJoinPoint joinPoint) {try {System.out.println("查詢前鼓鼓掌。");Object obj = joinPoint.proceed();System.out.println("查詢后鼓鼓掌。");return obj;} catch (Throwable e) {e.printStackTrace();}return null;}@AfterThrowing(value="AspectJAop.updateExceptionPoint()", throwing="ex")public void updateException(Throwable ex) {System.out.println("更新發(fā)生了異常:" + ex.toString());}@After(value="AspectJAop.updateFinallyPoint()")public void myFinally() {System.out.println("更新方法發(fā)生了異常, 最終通知一樣會執(zhí)行完成。");}@Pointcut(value="execution(* com.lywgames.dao.impl.UserDaoImpl.select(..))")private void selectPoint() {}@Pointcut(value="execution(* com.lywgames.dao.impl.UserDaoImpl.update(..))")private void updateExceptionPoint() {}@Pointcut(value="execution(* com.lywgames.dao.impl.UserDaoImpl.update(..))")private void updateFinallyPoint() {} }

4. 創(chuàng)建AopAction.java測試類

package com.lywgames.action;import org.springframework.context.support.ClassPathXmlApplicationContext; import com.lywgames.dao.impl.UserDaoImpl;public class AopAction {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");UserDaoImpl userDaoImpl = context.getBean(UserDaoImpl.class);userDaoImpl.insert();userDaoImpl.delete();userDaoImpl.select();userDaoImpl.update();context.close();} }

5. 在src目錄下添加applicationContext.xml配置

6. 運行項目

總結(jié)

以上是生活随笔為你收集整理的011_AOP注解开发的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。