spring-aop

本文介绍了Spring AOP的依赖引入、配置详解,以及如何在UserMapper和UserMapperImpl中实现AOP的通知(前置、后置、环绕、异常和返回)。通过XML配置和注解方式展示了如何进行切点匹配和通知执行。
摘要由CSDN通过智能技术生成

spring-aop(依赖,配置,实现)

布局
在这里插入图片描述

依赖项引入

<dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.9.6</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.6</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains</groupId>
            <artifactId>annotations</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

aop配置文件`

<?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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">
       </bans>

userMapper

package com.a2003.dao;

public interface UserMapper {
    void addUser();
    void deleteUser();
    void updateUser();
    void selectUser();

}


UserMapperImpl

package com.a2003.dao.impl;

import com.a2003.dao.UserMapper;

public class UserMapperImpl implements UserMapper {
    @Override
    public void addUser() {
        System.out.println("调用了addUser");
    }

    @Override
    public void deleteUser() {
        System.out.println("调用了daleteUser");

    }

    @Override
    public void updateUser() {
        System.out.println("调用了updateUser");

    }

    @Override
    public void selectUser() {
        System.out.println("调用了selectUser");

    }
}

XmlAdvice(注解方法可用)

package com.a2003.aspect;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

@Aspect
public class XmlAdvice {
    @Pointcut("execution(* com.a2003.dao.impl.UserMapperImpl.*(..))")
    //前置通知
    @Before("pointcut()")
    void before(JoinPoint joinPoint){
        System.out.println("这是前置控制");
        System.out.println("目标:"+joinPoint.getTarget());
        System.out.println("被植入增强处理的目标方法:"+joinPoint.getSignature());
    }
    //后置通知
    @Before("pointcut()")
    void after(){
        System.out.println("后置通知!!!");
    }

    //环绕通知
    @Around("pointcut()")
    Object around(ProceedingJoinPoint point ){
        Object object = null;
        System.out.println("环绕之前的内容!!!");
        try {
            object = point.proceed();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
        System.out.println("环绕之后的内容!!!");
        return object;
    }

    //异常通知
    @AfterThrowing("pointcut()")
    void afterExpection(){
        System.out.println("异常通知!!!");
    }

    //返回通知
    @AfterReturning("pointcut()")
    void afterReturning(JoinPoint joinPoint){
        System.out.println("只是返回通知(方法不一场的时候使用)");
        System.out.println("被植入增强处理的目标方法:"+joinPoint.getSignature());

    }
}

MyTest

import com.a2003.dao.UserMapper;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test1(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserMapper userMapper = applicationContext.getBean("userMapper", UserMapper.class);
        userMapper.selectUser();
    }
}

applicationContext

<?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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="userMapper" class="com.a2003.dao.impl.UserMapperImpl" > </bean>
    <bean id="xmlAdvice" class="com.a2003.aspect.XmlAdvice"></bean>

    <!--<aop:config proxy-target-class="true">
        &lt;!&ndash;切入点&ndash;&gt;
        <aop:pointcut id="pointcut" expression="execution(* com.a2003.dao.impl.UserMapperImpl.*(..))"/>
        &lt;!&ndash;指定切面 &ndash;&gt;
        <aop:aspect ref="xmlAdvice">
            <aop:before method="before" pointcut-ref="pointcut"></aop:before>&lt;!&ndash;前置通知&ndash;&gt;
            <aop:after method="after" pointcut-ref="pointcut"></aop:after>&lt;!&ndash;后置通知&ndash;&gt;
            <aop:after-throwing method="afterExpection" pointcut-ref="pointcut"></aop:after-throwing>&lt;!&ndash;异常通知&ndash;&gt;
            <aop:around method="around" pointcut-ref="pointcut"></aop:around>&lt;!&ndash;环绕通知&ndash;&gt;
            <aop:after-returning method="afterReturning" pointcut-ref="pointcut"></aop:after-returning>&lt;!&ndash;返回通知&ndash;&gt;
            </aop:aspect>
    </aop:config>--><!--aop寻常方法实现-->

    <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy><!--注解方法实现-->
</beans>

运行
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

临桉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值