spring AOP概念,原理,操作

AOP概念

aop:
1、面向切面编程,扩展功能不通过修改代码来实现。
2、aop采取横向抽取机制,取代了传统纵向继承体系重复性代码

AOP原理:


aop:底层通过动态代理来实现:

第二种情况:没有接口,创建子类的代理对象

AOP:操作术语

连接点:类里面的哪些方法可以被增强,这些方法成为连接点
切入点:类里面可以有很多的方法可以被增强,实际操作中只是增强了类里面的add()和update(),实际增强的方法成为切入点
通知/增强:增强的逻辑,成为增强。比如扩展日志功能,这个日志功能成为增强
  1. 前置通知:在方法之前执行
  2. 后置通知:在方法之后执行
  3. 异常通知:方法出现异常
  4. 最终通知:在后置之后执行
  5. 环绕通知:在方法之前和之后执行
切面:把增强应用到具体的方法上面,过程成为切面。把增强用到切入点过程

spring的AOP操作:

通过Aspectj来实现,有两种方式:
  1. 基于Aspectj的xml配置
  2. 基于Aspectj的注解
AOP的准备操作:
1、导包:
除了基本的jar包,还有aop jar包

2、导入aop的约束:
file:///G:/spring-framework-4.3.7.RELEASE/docs/spring-framework-reference/html/xsd-configuration.html

使用表达式配置切入点:
1、切入点:实际增强的方法
2、常用的表达式:
 execution(<访问修饰符>?<返回类型><方法名>(参数)<异常>)
(1) execution(* cn.itcast.aop.Book.add(..))
(2) execution(* cn.itcast.aop.Book.*(..))
(3) execution(* *.*(..))
即第一个*代表修饰符,*空格+增强方法全路径
匹配所有save开头的方法:execution(* save*(..))
Book.java
public class Book {
	public void add()
	{
		System.out.println("add()");
	}

}
MyBook.java
public class MyBook {
	public void before1()
	{
		System.out.println("before");
	}

}
aop.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 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 definitions here -->
<!--1 配置对象 -->
<bean id="book" class="cn.itcast.aop.Book"></bean>
<bean id="myBook" class="cn.itcast.aop.MyBook"></bean>
<!-- 2 配置aop操作 -->
<aop:config>
	<!--2.1  配置切入点 -->
	<aop:pointcut expression="execution(* cn.itcast.aop.Book.*(..))" id="pointcut1"/>
	<!-- 2.2 配置切面 -->
	<aop:aspect ref="myBook">
	<!-- 配置增强类型 method:增强类中的哪个方法为前置 -->
		<aop:before method="before1" pointcut-ref="pointcut1"/>
	</aop:aspect>
</aop:config>
</beans>
测试:
public class TestAop {

	public static void main(String[] args) {

		ApplicationContext context = new ClassPathXmlApplicationContext(
				"aop.xml");

		Book book = (Book) context.getBean("book");

		book.add();
	}
}
增加后置通知与环绕通知:
Book.java
public class Book {
	public void add()
	{
		System.out.println("add()");
	}

}
MyBook.java
public class MyBook {
	public void before1()
	{
		System.out.println("before");
	}
	public void after1()
	{
		System.out.println("after");
	}
	public void around1(ProceedingJoinPoint pjp) throws Throwable
	{
		System.out.println("before");
		pjp.proceed();
		System.out.println("after");
	}

}
aop.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 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 definitions here -->
<!--1 配置对象 -->
<bean id="book" class="cn.itcast.aop.Book"></bean>
<bean id="myBook" class="cn.itcast.aop.MyBook"></bean>
<!-- 2 配置aop操作 -->
<aop:config>
	<!--2.1  配置切入点 -->
	<aop:pointcut expression="execution(* cn.itcast.aop.Book.*(..))" id="pointcut1"/>
	<!-- 2.2 配置切面 -->
	<aop:aspect ref="myBook">
	<!-- 配置增强类型 method:增强类中的哪个方法为前置 -->
		<aop:before method="before1" pointcut-ref="pointcut1"/>
		<aop:after-returning method="after1" pointcut-ref="pointcut1"/>
		<aop:around method="around1" pointcut-ref="pointcut1" />
		
	</aop:aspect>
</aop:config>
</beans>
测试:
public class TestAop {

	public static void main(String[] args) {

		ApplicationContext context = new ClassPathXmlApplicationContext(
				"aop.xml");

		Book book = (Book) context.getBean("book");

		book.add();
	}
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值