基于代理类的AOP实现

本文介绍了Spring中的AOP代理如何使用ProxyFactoryBean创建,并详细讲解了Spring的通知类型,包括MethodInterceptor、MethodBeforeAdvice、AfterReturingAdvice、ThrowsAdvice和IntroductionInterceptor。接着,通过实战步骤展示了如何配置和使用ProxyFactoryBean,包括引入依赖、创建接口和目标类、定义切面以及配置和测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言


通过前面的两篇文章,相信大家对Spring中两种模式已经有了一定的了解。实际上,Spring中的AOP代理默认就是使用JDK动态代理的方式来实现的。在Spring中,使用ProxyFactoryBean是创建AOP代理的最基本方法,接下来我会对Spring中基于代理类的AOP实现的相关进行详细详解。

Spring的通知类型

1.org.aopalliance.intercept.Methodinterceptor(环绕通知)
在目标方法执行前后实施增强,可以应用于日志,事务管理等功能
2.org.aopalliance.intercept.MethodBeforeAdvice(前置通知)
在目标方法执行前实施增强,可以应用于权限管理等功能
3.org.aopalliance.intercept.AfterReturingAdvice(后置通知)
在目标方法执行后实施增强,可以应用于关闭流,上传文件,删除临时文件等功能
4.org.aopalliance.intercept.ThrowsAdvice(异常通知)
在方法抛出异常后实施增强,可以应用于处理异常记录日志等功能
5.org.aopalliance.intercept.IntroductionInterceptor(引介通知)
在目标方法添加一些新的方法和属性,可以应用于修改老版本程序(增强类)

ProxyFactoryBean类中的常用可配置属性

属性名称描述
target代理的目标对象
proxyInterfaces代理要实现的接口,如果是多个接口,可以使用以下格式赋值…
proxyTargetClass是否对类代理而不是接口,设置为ture是,使用CGLIB代理
interceptorNames需要织入目标的Advice
singleton返回的代理是否是单实例
optimize当设置为true时,强制使用CGLIB

实战

1.引入依赖

 <dependency>
      <groupId>aopalliance</groupId>
      <artifactId>aopalliance</artifactId>
      <version>1.0</version>
    </dependency>
    此处aop的依赖省略

1.创建接口UserDao类

package com.aop.jdk;

public interface UseDao {
    public void addUser();
    public void deleteUser();
}

2.创建目标类UserDaoImpl实现UserDao接口

package com.aop.jdk;
import com.ioc.UserDao;

//目标类
public class UserDaoImpl implements UseDao {
    @Override
    public void addUser() {
        System.out.println("添加用户");
    }

    @Override
    public void deleteUser() {
        System.out.println("删除用户");
    }
}

3.创建切面类

public class MyAspect implements MethodInterceptor{
    @Override
    public Object invoke(MethodInvocation mi) throws Throwable {
        check_Permission();
        //执行目标方法
        Object obj = mi.proceed();
        log();
        return obj;
    }
    public void check_Permission(){
        System.out.println("模拟检查权限。。。");
    }
    public void log(){
        System.out.println("模拟记录日志。。。");
    }

}

4.factorybean.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"

       xsi:schemaLocation="http://www.springframework.org/schema/beans
	                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	                   "
       default-lazy-init="false">
	<!--目标类-->
	<bean id="userDao" class="com.aop.factorybean.UserDaoImpl"></bean>
	<!--切面类-->
	<bean id="myAspect" class="com.aop.factorybean.MyAspect"></bean>
	<bean id="userDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
		<!--指定代理实现的接口-->
		<property name="proxyInterfaces" value="com.aop.factorybean.UseDao"></property>
		<!--指定目标对象-->
		<property name="target" ref="userDao"></property>
		<!--指定切面,植入环绕通知-->
		<property name="interceptorNames" value="myAspect"></property>
		<!--指定代理方式,rtue:使用cglib,false(默认):使用jdk动态代理-->
		<property name="proxyTargetClass" value="true"></property>
	</bean>
</beans>

5.创建测试类

public class ProxyFactoryBeanTest {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("factorybean.xml");
        UserDaoImpl userDao = (UserDaoImpl) applicationContext.getBean("userDaoProxy");
        userDao.addUser();
        userDao.deleteUser();
    }
}

运行结果
image

扫描关注下面二维码获得更多有用的资源!
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值