怎么用Spring框架实现AOP

发布时间:2022-09-05 09:38:24 作者:iii
来源:亿速云 阅读:105

这篇文章主要介绍了怎么用Spring框架实现AOP的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇怎么用Spring框架实现AOP文章都会有所收获,下面我们一起来看看吧。

第一种AOP实现方式

AfterLog

package com.xxx.demo.service1;

import org.junit.After;
import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice {
    @Override
    //returnValue:返回值
    public void afterReturning(Object returnValue, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println(
                "执行了"+method.getName()+"返回的结果:"+returnValue
        );
    }
}

Log

package com.xxx.demo.service1;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

//前置通知
public class log implements MethodBeforeAdvice {
    @Override
    //method:要执行的目标对象的方法 args:参数 target:目标读写
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
    }
}

配置文件

applicationContext.xml

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

<!--    注册bean-->
    <bean id="userService" class="com.xxx.demo.service1.UserServicelmp"></bean>
    <bean id="log" class="com.xxx.demo.service1.log"></bean>
    <bean id="afterLog" class="com.xxx.demo.service1.AfterLog"></bean>


<!--    配置aop:需要导入aop的约束-->
    <aop:config>
        <!--        切入点:expression:表达式,execution(要执行的位置!* * * *)-->
        <aop:pointcut id="pointcut" expression="execution(* com.xxx.demo.service1.UserServicelmp.*(..))"/>

<!--        执行环绕增加  把log的类添加到切入点里面-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"></aop:advisor>
    </aop:config>
</beans>

实例调用

package com.xxx.demo.service1;

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 =(UserService) context.getBean("userService");
        userService.add();
        userService.delete();
        userService.select();
        userService.update();
    }
}

定义接口

package com.xxx.demo.service1;

public class UserServicelmp implements UserService{
    @Override
    public void add() {
        System.out.println("增加了一个用户");
    }

    @Override
    public void delete() {
        System.out.println("删除了一个用户");
    }

    @Override
    public void update() {
        System.out.println("更新了一个用户");
    }

    @Override
    public void select() {
        System.out.println("查询了一个用户");
    }
}

怎么用Spring框架实现AOP

第二种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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

<!--    注册bean-->
    <bean id="userService" class="com.xxx.demo.service1.UserServicelmp"></bean>
    <bean id="log" class="com.xxx.demo.service1.log"></bean>
    <bean id="afterLog" class="com.xxx.demo.service1.AfterLog"></bean>
<!--    方式二:自定义类-->
    <bean id="diy" class="com.xxx.demo.service1.DiyPointCut"></bean>
    <aop:config>
<!--        自定义切面 ref 要引用的类-->
        <aop:aspect ref="diy">
<!--            切入点-->
            <aop:pointcut id="point" expression="execution(* com.xxx.demo.service1.UserServicelmp.*(..))"/>
<!--                通知-->
            <aop:before method="before" pointcut-ref="point"></aop:before>
            <aop:after method="after" pointcut-ref="point"></aop:after>
        </aop:aspect>
    </aop:config>
</beans>

怎么用Spring框架实现AOP

关于“怎么用Spring框架实现AOP”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“怎么用Spring框架实现AOP”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. Spring框架AOP的使用及个人对底层原理的理解
  2. Spring框架如何实现AOP添加日志记录功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring aop

上一篇:基于Python如何实现中秋佳节月饼抢购

下一篇:Django ORM多表查询怎么实现

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》