3、AOP技术

该栏目会系统的介绍Spring的知识体系,共分为基础部分、源代码和综合实例等模块,有兴趣的小伙伴可以关注下,如有疑问可以多多评论



AOP简介

1、概述

  • 面向切面编程,指在扩展功能时不需要修改源代码。

2、优点

  • 减少重复代码
  • 提高开发效率
  • 维护方便

3、原理

  • 有接口时,使用动态代理创建接口的实现类的代理对象,此时动态代理对象有着和实现类一样的功能,这叫JDK动态代理
  • 没有接口时,使用动态代理创建需要扩展功能的类的子类的代理对象,这叫CGLIB动态代理

4、操作术语

  • JoinPoint(连接点): 类中那些可以被增强的方法,被称为连接点
  • Pointcut(切入点)实际被增加的方法,被称为切入点
  • Advice(通知/增强):实际增强逻辑
类型功能
前置通知方法执行之前执行
后置通知方法执行之后执行
异常通知方法出现异常时执行
最终通知在后置通知之后执行
环绕通知在方法之前和方法之后执行
  • Aspect(切面):把增强逻辑应用到具体的方法上
  • Introduction(引介):在类运行期间动态的添加方法和属性
  • Target(目标对象):增强方法所在的类
  • Weaving(织入):把advice应用到target的过程
  • Proxy(代理):类被AOP织入增强后,产生的结果代理类

AspectJ框架

1、概述

  • :是一个面向切面的框架,与Spring框架结合操作AOP

2、相关Jar包

  • spring核心jar包、spring-aop.jar
  • spring-aspectjs.jar
  • aspectjweaver.jar
  • aopalliance.jar

3、使用方式

  • 配置文件方式

    • 添加aop相关约束
    • 使用表达式配置切入点:execution(<访问修饰符>?<返回值><方法名>(<参数>)<异常>)
  • 注解方式

    • 在Spring核心配置文件中,开启aop操作
    • 在方法上面使用注解

4、配置文件方式案例

  • 被增强类
public class Book {

	public void add(){
		System.out.println("add........");
	}

}
  • 增强类
public class MyBook {

	public void befored1(){
		System.out.println("前置增强............");
	}
	
	public void arround1(ProceedingJoinPoint proceedingJoinPoint)
			throws Throwable {
		System.out.println("先执行的代码.....");
		proceedingJoinPoint.proceed();
		System.out.println("后执行的代码.....");
	}

}
  • 测试类
public class TestDemo01 {

	@Test
	public void test01(){
		ApplicationContext context = 
					new ClassPathXmlApplicationContext("ApplicationContext.xml");
		Book book = (Book) context.getBean("book");
		book.add();
	}
	
}
  • 配置文件
<?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 
        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="book" class="cn.itcast.web.aop.Book"></bean>
	<bean id="myBook" class="cn.itcast.web.aop.MyBook"></bean>

	<!-- 配置aop -->
	<aop:config>
		<!-- 1.配置切入点 -->
		<aop:pointcut expression="execution(* cn.itcast.web.aop.Book.*(..))"
			id="pointcut1" />

		<!-- 2.配置切面 -->
		<aop:aspect ref="myBook">
			<aop:before method="befored1" pointcut-ref="pointcut1"/>
		</aop:aspect>
		
		<aop:aspect ref="myBook">
			<aop:around method="arround1" pointcut-ref="pointcut1"/>
		</aop:aspect>
	</aop:config>
</beans>

5、注解方式案例

  • 被增强类
public class Book {

	public void add(){
		System.out.println("add........");
	}

}
  • 增强类
@Aspect
public class MyBook {

	// 在方法上面使用注解
	@Before(value = "execution(* cn.itcast.aop.Book.*(..))")
	public void before1() {
		System.out.println("前置增强");
	}

}
  • 测试类
public class TestAop {

	@Test
	public void test01(){
		ApplicationContext context = 
					new ClassPathXmlApplicationContext("ApplicationContext.xml");
		Book book = (Book)context.getBean("book");
		book.add();
	}

}
  • 配置文件
<?xml version="1.0" encoding="UTF-8"?>
	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 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd">

	<!-- 开启aop操作 -->
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

	<!-- 创建对象 -->
	<bean id="book" class="cn.itcast.aop.Book"></bean>
	<bean id="myBook" class="cn.itcast.aop.MyBook"></bean>
</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值