Spring的AOP

Sping的AOP

1.Spring的AOP简介

  • AOPAspect Oriented Programming 的缩写,意思为面向切面编程,是通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。

  • 作用:在程序运行期间,在不修改源码的情况下对方法进行功能增强

  • 优势:减少重复代码,提高开发效率,并且便于维护

  • 底层实现:AOP 的底层是通过 Spring 提供的的动态代理技术实现的。在运行期间,Spring通过动态代理技术动态的生成代理对象,代理对象方法执行时进行增强功能的介入,在去调用目标对象的方法,从而完成功能的增强。

常用的动态代理技术

  • JDK 代理 : 基于接口的动态代理技术
  • cglib 代理:基于父类的动态代理技术

知识要点

  • aop:面向切面编程

  • aop底层实现:基于JDK的动态代理 和 基于Cglib的动态代理

  • aop的重点概念:

    Pointcut(切入点):被增强的方法

    Advice(通知/ 增强):封装增强业务逻辑的方法

    Aspect(切面):切点+通知

    Weaving(织入):将切点与通知结合的过程

  • 开发明确事项:

    谁是切点(切点表达式配置)

    谁是通知(切面类中的增强方法)

    将切点和通知进行织入配置

2.基于 XML 的 AOP 开发

快速入门

① 导入 AOP 相关坐标

②创建目标接口和目标类(内部有切点)

③ 创建切面类(内部有增强方法)

④ 将目标类和切面类的对象创建权交给 spring

⑤ 在 applicationContext.xml 中配置织入关系

⑥ 测试代码

① 导入 AOP 相关坐标

<!--导入spring的context坐标,context依赖aop--> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-context</artifactId> 
    <version>5.0.5.RELEASE</version>
</dependency>
<!-- aspectj的织入 --> 
<dependency> 
    <groupId>org.aspectj</groupId> 
    <artifactId>aspectjweaver</artifactId> 
    <version>1.8.13</version>
</dependency>

② 创建目标接口和目标类(内部有切点)

public interface TargetInterface {
	public void method();
}
public class Target implements TargetInterface {
    @Override
    public void method() {
    	System.out.println("Target running....");
	}
}

③ 创建切面类(内部有增强方法)

public class MyAspect {
    //前置增强方法
    public void before(){
    	System.out.println("前置代码增强.....");
	} 
}

④ 将目标类和切面类的对象创建权交给 spring

<!--配置目标类--> 
<bean id="target" class="com.ropz.aop.Target"></bean>
<!--配置切面类--> 
<bean id="myAspect" class="com.ropz.aop.MyAspect"></bean>

⑤ 在 applicationContext.xml 中配置织入关系

导入aop命名空间

配置切点表达式和前置增强的织入关系

<aop:config>
	<!--引用myAspect的Bean为切面对象--> 
    <aop:aspect ref="myAspect">
		<!--配置Target的method方法执行时要进行myAspect的before方法前置增强--> 				<aop:before method="before" pointcut="execution(public void 
com.ropz.aop.Target.method())"></aop:before>
	</aop:aspect>
</aop:config>

⑥ 测试代码

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml")
public class AopTest { 
    @Autowired
	private TargetInterface target;
	@Test
	public void test1(){
	target.method();
	} 
}

XML 配置 AOP 详解

通知的配置语法:

<aop:通知类型 method=“切面类中方法名” pointcut=“切点表达式"></aop:通知类型>

切点表达式的抽取

当多个增强的切点表达式相同时,可以将切点表达式进行抽取,在增强中使用 pointcut-ref 属性代替 pointcut 属性来引用抽取后的切点表达式。

<aop:config>
	<!--引用myAspect的Bean为切面对象--> 
    <aop:aspect ref="myAspect"> 
        <aop:pointcut id="myPointcut" expression="execution(* com.ropz.aop.*.*(..))"/>
		<aop:before method="before" pointcut-ref="myPointcut"></aop:before>
	</aop:aspect>
</aop:config>

知识要点

  • 切点表达式的写法:

    execution([修饰符] 返回值类型 包名.类名.方法名(参数))
    
  • aop织入的配置

    <aop:config> 
        <aop:aspect ref=“切面类”> 
            <aop:before method=“通知方法名称” pointcut=“切点表达式"></aop:before>
    	</aop:aspect>
    </aop:config>
    
  • 通知的类型:前置通知、后置通知、环绕通知、异常抛出通知、最终通知

3.基于注解的 AOP 开发

快速入门

① 创建目标接口和目标类(内部有切点)

public interface TargetInterface {
	public void method();
}
public class Target implements TargetInterface {
    @Override
    public void method() {
    	System.out.println("Target running....");
	}
}

② 创建切面类(内部有增强方法)

public class MyAspect {
	//前置增强方法
	public void before(){
		System.out.println("前置代码增强.....");
	} 
}

③ 将目标类和切面类的对象创建权交给 spring

@Component("target")
public class Target implements TargetInterface {
	@Override
	public void method() {
		System.out.println("Target running....");
	} 
}
@Component("myAspect")
public class MyAspect {
	public void before(){
		System.out.println("前置代码增强.....");
	}
}

④ 在切面类中使用注解配置织入关系

@Component("myAspect")
@Aspect
public class MyAspect {
	@Before("execution(* com.itheima.aop.*.*(..))")
	public void before(){
		System.out.println("前置代码增强.....");
	}
}

⑤ 在配置文件中开启组件扫描和 AOP 的自动代理


<!--组件扫描--> 
<context:component-scan base-package="com.ropz.aop"/>
<!--aop的自动代理--> 
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

⑥ 测试

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml")
public class AopTest { @Autowired
	private TargetInterface target;
	@Test
	public void test1(){
		target.method();
	}
}

注解配置 AOP 详解

通知的配置语法:@通知注解(“切点表达式")

切点表达式的抽取

@Component("myAspect")
@Aspect
public class MyAspect {
    @Before("MyAspect.myPoint()")
    public void before(){
    System.out.println("前置代码增强.....");
    }
    @Pointcut("execution(* com.ropz.aop.*.*(..))")
    public void myPoint(){}
}

知识要点

  • 注解aop开发步骤

    • 使用@Aspect标注切面类
    • 使用@通知注解(“切点表达式")标注通知方法
    • 在配置文件中开启组件扫面和配置aop自动代理
posted @   乐生xXx  阅读(28)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
点击右上角即可分享
微信分享提示