带你了解Spring AOP的使用详解

 更新时间:2021年07月12日 09:22:19   作者:wbcra  
这篇文章主要介绍了Spring AOP的使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

springmvc.xml

<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"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    https://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    https://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    https://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    https://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/mvc
    https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <bean id="adminCheck" class="cn.hp.impl.AdminCheck"></bean>
    <bean id="transmaction" class="cn.hp.impl.Transmaction"></bean>
    <bean id="bankDao" class="cn.hp.impl.BankDaoImpl">
<!--        <property name="adminCheck" ref="adminCheck"></property>-->
<!--        <property name="transmaction" ref="transmaction"></property>-->
    </bean>
<!--    <aop:config>-->
<!--&lt;!&ndash;        切入点,什么时候,执行什么切入进来&ndash;&gt;-->
<!--        <aop:pointcut id="savepoint" expression="execution(* cn.hp.impl.*.*(..))"/>-->
<!--&lt;!&ndash;        切面-用来做事情-执行业务逻辑之前-你要做或执行什么事情&ndash;&gt;-->
<!--        <aop:aspect id="adminincepter" ref="adminCheck">-->
<!--            <aop:before method="check" pointcut-ref="savepoint"></aop:before>-->
<!--        </aop:aspect>-->
<!--        -->
<!--        <aop:aspect id="transmactionincepter" ref="transmaction">-->
<!--            <aop:around method="dointcepter" pointcut-ref="savepoint"></aop:around>-->
<!--        </aop:aspect>-->
<!--    </aop:config>-->
    <bean id="logInfo" class="cn.hp.impl.LogInfo"></bean>
    <bean id="adminCheckInterceptor" class="cn.hp.interceptor.AdminCheckInterceptor">
        <property name="adminCheck" ref="adminCheck"></property>
    </bean>
    <bean id="logInfoInceptor" class="cn.hp.interceptor.LogInfoInceptor">
        <property name="logInfo" ref="logInfo"></property>
    </bean>
    <bean id="transmactionInterceptor" class="cn.hp.interceptor.TransmactionInterceptor">
        <property name="transmaction" ref="transmaction"></property>
    </bean>
<!--    注入代理类-->
    <bean id="proxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--代理对象是谁-->
        <property name="target" ref="bankDao"></property>
<!--        代理的接口-->
        <property name="proxyInterfaces" value="cn.hp.dao.BankDao"></property>
<!--        代理的通知组件-->
        <property name="interceptorNames">
            <list>
                <value>adminCheckInterceptor</value>
                <value>logInfoInceptor</value>
                <value>transmactionInterceptor</value>
            </list>
        </property>
    </bean>



</beans>

BankDao

package cn.hp.dao;
public interface BankDao {
    /**
     * 存钱
     */
    public void remit();
    /**
     * 取钱
     */
    public void withdrawMoney();
    /**
     * 转账
     */
    public void transferAccounts();
}

AdminCheck

package cn.hp.impl;
public class AdminCheck {
    public void check(){
        System.out.println("正在验证账号信息");
    }
}

BankDaoImpl

package cn.hp.impl;
import cn.hp.dao.BankDao;
public class BankDaoImpl implements BankDao {

    @Override
    public void remit() {
//        adminCheck.check();
//        transmaction.beginTransmaction();
        System.out.println("存钱的业务逻辑");
//        transmaction.closeTransmaction();
    }
    @Override
    public void withdrawMoney() {
//        adminCheck.check();
//        transmaction.beginTransmaction();
        System.out.println("取钱的业务逻辑");
//        transmaction.closeTransmaction();
    }
    @Override
    public void transferAccounts() {
        System.out.println("转账的业务逻辑");
    }
}

LogInfo

package cn.hp.impl;
public class LogInfo {
    public void write(){
        System.out.println("写日志中");
    }
}

Transmaction

package cn.hp.impl;
import org.aspectj.lang.ProceedingJoinPoint;
public class Transmaction {
    public void beginTransmaction(){
        System.out.println("开始事务");
    }
    public void closeTransmaction(){
        System.out.println("结束事务");
    }
    public void dointcepter(ProceedingJoinPoint point) throws Throwable {
        beginTransmaction();
        point.proceed();
        closeTransmaction();
    }
}

AdminCheckInterceptor

package cn.hp.interceptor;
import cn.hp.impl.AdminCheck;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
import java.util.Set;
public class AdminCheckInterceptor implements MethodBeforeAdvice {
    private AdminCheck adminCheck;
    public AdminCheck getAdminCheck() {
        return adminCheck;
    }
    public void setAdminCheck(AdminCheck adminCheck) {
        this.adminCheck = adminCheck;
    }
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        adminCheck.check();
    }
}

LogInfoInceptor

package cn.hp.interceptor;
import cn.hp.impl.LogInfo;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class LogInfoInceptor implements AfterReturningAdvice {
    private LogInfo logInfo;
    public LogInfo getLogInfo() {
        return logInfo;
    }
    public void setLogInfo(LogInfo logInfo) {
        this.logInfo = logInfo;
    }
    @Override
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        logInfo.write();
    }
}

TransmactionInterceptor

package cn.hp.interceptor;
import cn.hp.impl.Transmaction;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.ibatis.transaction.Transaction;
public class TransmactionInterceptor implements MethodInterceptor {
    private Transmaction transmaction;
    public Transmaction getTransmaction() {
        return transmaction;
    }
    public void setTransmaction(Transmaction transmaction) {
        this.transmaction = transmaction;
    }
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        transmaction.beginTransmaction();
        Object obj = invocation.proceed();
        transmaction.closeTransmaction();
        return obj;
    }
}

Test

package cn.hp.test;
import cn.hp.dao.BankDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
    public static void main(String[] args) {
        //test1();
//        test2();
        test3();
    }
    private static void test3() {
        ApplicationContext ac= new ClassPathXmlApplicationContext("springmvc.xml");
        BankDao proxyFactory = ac.getBean("proxyFactory", BankDao.class);
        proxyFactory.remit();
    }
    private static void test2() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("springmvc.xml");
        BankDao bd = ac.getBean("bankDao",BankDao.class);
        bd.transferAccounts();
    }
    private static void test1() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("springmvc.xml");
        BankDao bd = ac.getBean("bankDao",BankDao.class);
        bd.withdrawMoney();
    }
}

总结

本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注脚本之家的更多内容!

相关文章

  • 一文详解MVCC的执行原理

    一文详解MVCC的执行原理

    MVCC是一种并发控制机制,用于解决数据库并发访问中,数据一致性问题,它通过在读写操作期间保存多个数据版本,以提供并发事务间的隔离性,本文将和大家简单聊聊MVCC的执行原理,需要的朋友可以参考下
    2023-12-12
  • @Conditional注解的使用场景和源码解析

    @Conditional注解的使用场景和源码解析

    这篇文章主要介绍了@Conditional注解的使用场景和源码解析,@Conditional是一个条件注解,它的作用是判断Bean是否满足条件,如果满足条件,则将Bean注册进IOC中,如果不满足条件,则不进行注册,需要的朋友可以参考下
    2023-11-11
  • 解决工具接口调用报错:error:Unsupported Media Type问题

    解决工具接口调用报错:error:Unsupported Media Type问题

    当遇到"UnsupportedMediaType"错误时,意味着HTTP请求的Content-Type与服务器期望的不匹配,比如服务器期待接收JSON格式数据,而发送了纯文本格式,常见的Content-Type类型包括text/html、application/json、multipart/form-data等
    2024-10-10
  • JMagick实现基本图像处理的类实例

    JMagick实现基本图像处理的类实例

    这篇文章主要介绍了JMagick实现基本图像处理的类,实例分析了java图像处理的相关技巧,需要的朋友可以参考下
    2015-06-06
  • 详解java定时任务

    详解java定时任务

    这篇文章主要为大家详细介绍了java定时任务,使用JDK中的Timer定时任务来实现,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • Reactive反应式编程及使用介绍

    Reactive反应式编程及使用介绍

    这篇文章主要介绍了为什使用Reactive反应式编程的原因分析,有需要的朋友可以借鉴参考下,希望能够有所帮助祝大家多多进步,早日升职加薪
    2022-02-02
  • SpringMVC中拦截器的实现

    SpringMVC中拦截器的实现

    SpringMVC 中的 Interceptor 拦截器是非常重要和相当有用的,它的主要作用是拦截指定的用户请求,并进行相应的预处理与后处理,这篇文章主要介绍了SpringMVC的拦截器相关知识,需要的朋友可以参考下
    2022-01-01
  • Java顺时针打印矩阵

    Java顺时针打印矩阵

    这篇文章主要为大家详细介绍了Java顺时针打印矩阵,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-03-03
  • svn 清理失败 (cleanup 失败) 的快速解决方法

    svn 清理失败 (cleanup 失败) 的快速解决方法

    下面小编就为大家带来一篇svn 清理失败 (cleanup 失败) 的快速解决方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-10-10
  • 详解java封装返回结果与RestControllerAdvice注解

    详解java封装返回结果与RestControllerAdvice注解

    这篇文章主要为大家介绍了java封装返回结果与RestControllerAdvice注解实例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09

最新评论