Spring AOP的一些基本概念第一个aop程序

1.概念汇总:

        面向切面编程:
AOP是一种编程范式,隶属于软工范围。知道开发者如何组织程序结构
AOP弥补了OOP的不足,基于OOP基础之上进行横向开发
AOP联盟

基本概念:
连接点(Joinpoint):原始功能中的所有方法

切入点(Pointcut)某个连接点的动态执行过程(这些被抽取了共性功能的方法)

通知(advice)
共性功能抽取出来放到某个类中称这个类为通知类,这些具体功能称为通知
引入(introduce):
动态添加成员定义:
目标对象(Target Object):
程序运行时,原始对象缺失功能,原始对象称为目标对象,
代理对象是目标对象做代理得到的
AOP代理(AOP Proxy):
原始对象已经无法完成任务,实际运行时将不能完成完整的业务逻辑,(创建共性功能的类对象)
因此需要使用代理机制完成此功能,创建代理对象的过程不由程序员控制,
由AOP的核心完成整个过程称为AOP代理

织入(Weaving):
运行程序时,目标对象缺失功能,在实际运行过程中,
原始共性功能被挖取的位置被称为AOP的通知类别(前,后5种)将通知按照被挖取的位置(通知类别)
动态添加到指定好位置的过程称为织入
分为三个阶段:
1.编译时织入  生成的class带有通知
2.加载时织入  class加入jvm时将通知织入
3.运行时织入  jvm加载的class不具有通知,但是运行该类中的制定方法时
动态的添加通知(spring)
切面(Aspect):
将切入点与通知之间的关系进行指定,关系称为切面,切面描述的是切入点与通知之间的关系
<!-- <aop:before method="before" pointcut-ref="pt"/>
<aop:after method="after" pointcut-ref="pt"/> -->
<!-- <aop:after-returning method="afterReturning" pointcut-ref="pt"/> -->
<aop:around method="round" pointcut-ref="pt"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="pt"/>
通知的5种类型:执行顺序:相同类型已配置顺序为准
1.before:<aop:before method="before" pointcut-ref="pt"/>
2.after:<aop:after method="after" pointcut-ref="pt"/> 
3.afterReturning:返回后加入,有异常不执行<aop:after-returning method="afterReturning" pointcut-ref="pt"/>
4.afterThrowing:有异常才会执行<aop:after-throwing method="afterThrowing" pointcut-ref="pt"/>
5.round:<aop:around method="round" pointcut-ref="pt"/>
用法最广泛,可替代before,after,调用目标方法
public void round(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("round before");
pjp.proceed();
System.out.println("round after");

}

2.第一个aop程序:


    目标对象:

public class Mytarget {
	public void save() {
		System.out.println(2);
		System.out.println(3);
	}
	
	public void save1(int i) {
		System.out.println(22);
		System.out.println(33);
	}
}
通知:
public class MyAdvice {
	public void fn() {
		System.out.println(1);
	}
}

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:util="http://www.springframework.org/schema/util"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       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/util
	   http://www.springframework.org/schema/util/spring-util.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop.xsd">
		
	<!--spring管理资源都是bean的形式  -->
	<!-- 你的资源是那个类的对象 
		<bean class="类名" id="类名对应的唯一标识"></bean>
	-->
	<!--配置aop,切入点与通知之间的关系   _切面 -->
	<!--aop:config:配置aop  -->
	<aop:config>
		<aop:aspect ref="myAdvice">
			<!--配置aop的通知类别  
				aop:before method="" 通知类别的具体通知
				aop:before pointcut=""切入点
				<aop:before method="fn1" pointcut="执行到save方法时"/>
				
		 -->
		 	<!-- 在方法save的前面加入方法fn -->
			<aop:before method="fn" pointcut="execution(void com.array.aop.base.Mytarget.save())"/>
			<aop:after method="fn" pointcut="execution(void com.array.aop.base.Mytarget.save1())"/>
		</aop:aspect>
	</aop:config>
	<!--/将目标对象与通知定义为bean  -->
	<bean id="myAdvice" class="com.array.aop.base.MyAdvice"></bean>
	<bean id="myTarget" class="com.array.aop.base.Mytarget"></bean>
</beans>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值