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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

注解方式实现aop

發(fā)布時間:2023/12/10 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 注解方式实现aop 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

aop注解實現(xiàn)

    • spring配置文件
    • 目標(biāo)接口,目標(biāo)實現(xiàn)類,切面類 注解寫法
    • 使用spring-test測試

spring配置文件

<?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/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- spring 組件掃描 --><context:component-scan base-package="com.lovely.aop.anno"/><!-- aop 自動代理測試 --><aop:aspectj-autoproxy/></beans>

目標(biāo)接口,目標(biāo)實現(xiàn)類,切面類 注解寫法

package com.lovely.aop.anno;public interface TargetInterface {public abstract void save(); } package com.lovely.aop.anno;import org.springframework.stereotype.Component;/*** @author echo lovely* @date 2020/8/5 17:35*/@Component("target") public class Target implements TargetInterface {public void save() {/*try {System.out.println(1 / 0);} catch (Exception e) {System.out.println(e);}*/System.out.println("save running about aop...");} }
  • 聲明切面并進行織入
package com.lovely.aop.anno;import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component;/*** @author echo lovely*/ @Component("myAspect") @Aspect // 標(biāo)注當(dāng)前類是切面類 public class MyAspect {// 配置前置通知@Before(value = "execution(* com.lovely.aop.anno.*.*(..))")public void before() {System.out.println("前置增強...");}public void afterReturning() {System.out.println("后置增強...");}// @Around("execution(* com.lovely.aop.anno.*.*(..))")@Around("MyAspect.myPoint()")public Object around(ProceedingJoinPoint process) {System.out.println("環(huán)繞通知前...");Object obj = null;try {obj = process.proceed();} catch (Throwable throwable) {throwable.printStackTrace();}System.out.println("環(huán)繞通知后...");return obj;}public void afterThrowing() {System.out.println("異常拉...");}// 后置增強// @After("execution(* com.lovely.aop.anno.Target.*(..))")@After("myPoint()")public void after() {System.out.println("最終通知...");}// aop注解 抽取切點表達式@Pointcut("execution(* com.lovely.aop.anno.Target.*(..))")public void myPoint() {}}

使用spring-test測試

import com.lovely.aop.anno.TargetInterface; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/*** @author echo lovely* @date 2020/8/5 17:48*/ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContextAnno.xml") public class AopAnnotationTest {@Autowiredprivate TargetInterface target;@Testpublic void test1() {target.save();}} 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

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

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