AOP实现方式-P20,21,22
创始人
2024-02-17 17:30:15
0

 项目的包:

 pom依赖导入有关aop的包:

    org.aspectjaspectjweaver1.9.4

代理类交给spring来实现。


UserService:

package com.Li.service;public interface UserService {public void add();public void delete();public void update();public void select();
}

UserServiceImpl:

package com.Li.service;public class UserServiceImpl implements UserService{@Overridepublic void add() {System.out.println("增加了一个用户!");}@Overridepublic void delete() {System.out.println("删除了一个用户!");}@Overridepublic void update() {System.out.println("修改了一个用户!");}@Overridepublic void select() {System.out.println("查询了一个用户!");}
}

log:

package com.Li.log;import org.springframework.aop.MethodBeforeAdvice;import java.lang.reflect.Method;
//前置日志
public class log implements MethodBeforeAdvice {//method: 要执行的目标对象的方法//args:参数//target:目标对象@Overridepublic void before(Method method, Object[] args, Object target) throws Throwable {System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");}
}

AfterLog:

package com.Li.log;import org.springframework.aop.AfterReturningAdvice;import java.lang.reflect.Method;
//后置日志
public class AfterLog implements AfterReturningAdvice {//returnValue: 返回值.由于是AfterReturning,所以有返回值@Overridepublic void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {System.out.println("执行了"+method.getName()+"方法,返回结果为:"+returnValue);}
}

方式一:(使用spring的接口实现动态代理类)

applicationContext.xml:



MyTest:(测试)(不变)

import com.Li.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyTest {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");//动态代理代理的是接口:注意点UserService userService = context.getBean("userService", UserService.class);userService.add();}
}


方式二(主要是切面定义)

增加一个类:

DiyPointCut:

package com.Li.diy;public class DiyPointCut {public void before(){System.out.println("=====方法执行前=====");}public void after(){System.out.println("=====方法执行后=====");}}

applicationContext.xml:(看方式二)



测试(代码不变直接测试):

 


利用注解开发:

 

AnnotationPointCut:

package com.Li.diy;//方式三:使用注解方式实现AOPimport org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;@Aspect//标记这个类是一个切面
public class AnnotationPointCut {@Before("execution(* com.Li.service.UserServiceImpl.*(..))")public void before(){System.out.println("=====方法执行前=====");}@After("execution(* com.Li.service.UserServiceImpl.*(..))")public void after(){System.out.println("=====方法执行后=====");}}

 applicationContext.xml:

    

直接测试

 

三种方式都可以实现AOP。第一种全面,第二种便于理解,第三种方便。你可以自己选择自己喜欢的方式。

相关内容

热门资讯

思想政治工作条例最新修订内容,... 思想政治工作条例最新修订内容,思想政治工作条例全文下载 思想政治工作条例最新修订,全文下载与深度解读...
CBA潜力赛为何打成“老将赛”... 计时钟归零,双方教练握手致意,观众开始退场,CBA联赛的正赛宣告结束。然而球场并未就此沉寂,替补席上...
“手术钻头断裂遗留患者体内”,... 12月21日,湖南祁阳市卫生健康局发布情况通报称,近日,有媒体报道祁阳市中医医院发生骨科手术钻头断裂...
代驾纠纷 代驾时撞伤行人、车辆发生故障…… 这些都和车主无关,应由代驾赔偿? 观点: 使用代驾服务并非将所有...
公司股东与妻子分居期间出轨女下... 近日据报道,宁夏永宁县人民法院一审查明公司股东李某乙在与妻子李某甲分居期间,与公司女员工马某某存在不...
动物学家、律师和创作者,Thi... 12月21日,以“一起·了不起”为主题的2025 ThinkPad黑FUN礼在京举办。活动现场,律师...
徐奇渊:扩内需与对外政策紧密相... 近日,中国海关总署发布了一组数据令人关注:2025年前11个月,我国货物贸易顺差达到1.08万亿美元...
46岁上海独居女子不幸离世,官... 居住在上海虹口区46岁的蒋女士因突发脑溢血于今年10月入院,远亲吴先生与其公司共同垫付了医药费,但她...
威海市汽车以旧换新补贴政策调整... 根据稳妥有序开展消费品以旧换新工作统一部署,经研究决定,对我市汽车以旧换新补贴政策进行调整。现将有关...