• AOP实现方式-P20,21,22


     项目的包:

     pom依赖导入有关aop的包:

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.aspectjgroupId>
    4. <artifactId>aspectjweaverartifactId>
    5. <version>1.9.4version>
    6. dependency>
    7. dependencies>

    代理类交给spring来实现。


    UserService:

    1. package com.Li.service;
    2. public interface UserService {
    3. public void add();
    4. public void delete();
    5. public void update();
    6. public void select();
    7. }

    UserServiceImpl:

    1. package com.Li.service;
    2. public class UserServiceImpl implements UserService{
    3. @Override
    4. public void add() {
    5. System.out.println("增加了一个用户!");
    6. }
    7. @Override
    8. public void delete() {
    9. System.out.println("删除了一个用户!");
    10. }
    11. @Override
    12. public void update() {
    13. System.out.println("修改了一个用户!");
    14. }
    15. @Override
    16. public void select() {
    17. System.out.println("查询了一个用户!");
    18. }
    19. }

    log:

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

    AfterLog:

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

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

    applicationContext.xml:

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:aop="http://www.springframework.org/schema/aop"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans
    6. https://www.springframework.org/schema/beans/spring-beans.xsd
    7. http://www.springframework.org/schema/aop
    8. https://www.springframework.org/schema/aop/spring-aop.xsd">
    9. <bean id="userService" class="com.Li.service.UserServiceImpl"/>
    10. <bean id="log" class="com.Li.log.log"/>
    11. <bean id="afterLog" class="com.Li.log.AfterLog"/>
    12. <aop:config>
    13. <aop:pointcut id="pointcut" expression="execution(* com.Li.service.UserServiceImpl.*(..))"/>
    14. <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
    15. <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
    16. aop:config>
    17. beans>

    MyTest:(测试)(不变)

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


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

    增加一个类:

    DiyPointCut:

    1. package com.Li.diy;
    2. public class DiyPointCut {
    3. public void before(){
    4. System.out.println("=====方法执行前=====");
    5. }
    6. public void after(){
    7. System.out.println("=====方法执行后=====");
    8. }
    9. }

    applicationContext.xml:(看方式二)

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:aop="http://www.springframework.org/schema/aop"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans
    6. https://www.springframework.org/schema/beans/spring-beans.xsd
    7. http://www.springframework.org/schema/aop
    8. https://www.springframework.org/schema/aop/spring-aop.xsd">
    9. <bean id="userService" class="com.Li.service.UserServiceImpl"/>
    10. <bean id="log" class="com.Li.log.log"/>
    11. <bean id="afterLog" class="com.Li.log.AfterLog"/>
    12. <bean id="diy" class="com.Li.diy.DiyPointCut"/>
    13. <aop:config>
    14. <aop:aspect ref="diy">
    15. <aop:pointcut id="point" expression="execution(* com.Li.service.UserServiceImpl.*(..))"/>
    16. <aop:before method="before" pointcut-ref="point"/>
    17. <aop:after method="after" pointcut-ref="point"/>
    18. aop:aspect>
    19. aop:config>
    20. beans>

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

     


    利用注解开发:

     

    AnnotationPointCut:

    1. package com.Li.diy;
    2. //方式三:使用注解方式实现AOP
    3. import org.aspectj.lang.annotation.After;
    4. import org.aspectj.lang.annotation.Aspect;
    5. import org.aspectj.lang.annotation.Before;
    6. @Aspect//标记这个类是一个切面
    7. public class AnnotationPointCut {
    8. @Before("execution(* com.Li.service.UserServiceImpl.*(..))")
    9. public void before(){
    10. System.out.println("=====方法执行前=====");
    11. }
    12. @After("execution(* com.Li.service.UserServiceImpl.*(..))")
    13. public void after(){
    14. System.out.println("=====方法执行后=====");
    15. }
    16. }

     applicationContext.xml:

    1. <bean id="annotationPointCut" class="com.Li.diy.AnnotationPointCut"/>
    2. <aop:aspectj-autoproxy/>

    直接测试

     

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

  • 相关阅读:
    从 Vue2向Vue3的迁移
    结合均线分析k线图的基本知识
    【CPP】栈简介及简化模拟实现
    ssm+java+vue微信小程序的驾校预约管理系统#毕业设计
    Linux安装nginx详细步骤
    竞赛选题 基于机器视觉的手势检测和识别算法
    Flutter 使用 device_info_plus 遇到的问题
    [汇编语言王爽]笔记2--p18-p22
    MyBatis-Plus找不到Mapper.xml文件的解决方法
    gopsutil 获取服务端资源信息
  • 原文地址:https://blog.csdn.net/m0_54842832/article/details/127994455