Spring 的基于 AspectJ 的 AOP 开发(配置文件方式)

AOP 的开发中的相关术语

  • Joinpoint(连接点):所谓连接点是指那些被拦截到的点。在 spring 中,这些点指的是方法,因为 spring
    只支持方法类型的连接点.
  • Pointcut(切入点):所谓切入点是指我们要对哪些 Joinpoint 进行拦截的定义.
  • Advice(通知/增强):所谓通知是指拦截到Joinpoint之后所要做的事情就是通知.通知分为前置通知,后置通知,异常通知,最终通知,环绕通知(切面要完成的功能)
  • Introduction(引介):引介是一种特殊的通知在不修改类代码的前提下, Introduction
    可以在运行期为类动态地添加一些方法或 Field.
  • Target(目标对象):代理的目标对象
  • Weaving(织入):是指把增强应用到目标对象来创建新的代理对象的过程.
  • spring 采用动态代理织入,而 AspectJ 采用编译期织入和类装在期织入
  • Proxy(代理):一个类被 AOP 织入增强后,就产生一个结果代理类
  • Aspect(切面): 是切入点和通知(引介)的结合

引入相应的 jar 包

这些包哪里下,我前面已经说过,这里只给一下网址

  • Spring的开发包下载:https://repo.spring.io/webapp/#/artifacts/browse/tree/General/libs-release-local/org/springframework/spring
  • 其他包1:http://www.xwood.net/search-jar/?q=com.springsource.org.appche.log4j(有时候这里面的包会失效,就可以上下面这个网址找)
  • 其他包2:https://maven.aliyun.com/mvn/search
    在这里插入图片描述

在这里插入图片描述

AspectJ的通知类型

  • 前置通知 :在目标方法执行之前执行.
  • 后置通知 :在目标方法执行之后执行
  • 环绕通知 :在目标方法执行前和执行后执行
  • 异常抛出通知:在目标方法执行出现 异常的时候 执行
  • 最终通知 :无论目标方法是否出现异常 最终通知都会 执行
  • 在这里插入图片描述

AspectJ的切入点表达式

在这里插入图片描述
注意,表达式中加[ ]的部分表示可省略部分,各部分间用空格分开
在这里插入图片描述

开始演示

1、实现步骤

定义业务接口与实现类

package com.test.dao;

public interface OrderDao {
	void addOrder();
	void deleteOrder();
}
package com.test.dao.impl;

import com.test.dao.OrderDao;

public class OrderDaoImpl implements OrderDao{

	@Override
	public void addOrder() {
		System.out.println("添加订单");
	}

	@Override
	public void deleteOrder() {
		System.out.println("删除订单");
	}

}

定义切面类

package com.test.Aspect;

public class MyAspectXml {
	public void before() {
		System.out.println("前置增强!");
	}
	
	public void after() {
		System.out.println("后置增强!");
		System.out.println();
	}
}

2、配置

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
	   http://www.springframework.org/schema/aop
	   http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.2.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">    
       
       <!-- 将目标类交给Spring管理 -->
       <bean id="orderDao" class="com.test.dao.impl.OrderDaoImpl"></bean>
      
       <!-- 配置切面类 -->
       <bean id="myAspectXml" class="com.test.Aspect.MyAspectXml"></bean>
       
       <!-- 进行aop的配置 -->
       <aop:config>
       		<!-- 配置切入点表达式:哪些类的哪些方法需要进行增强 -->
       		<aop:pointcut expression="execution(* com.test.dao.impl.OrderDaoImpl.*(..))" id="pointcut1"/>
       		<!-- 配置切面 -->
       		<aop:aspect ref="myAspectXml">
       			//使用比较多的就是前置和后置
       			<aop:before method="before" pointcut-ref="pointcut1"/>
       			<aop:after method="after" pointcut-ref="pointcut1"/>
       		</aop:aspect>
       </aop:config>
</beans>

3、测试

package springpro1;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.test.dao.OrderDao;
//使用这两个注解就代替了ApplicationContext ap = new ClassPathXmlApplicationContext("applicationContext.xml");
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestDemo3 {
	@Autowired
	private OrderDao orderDao;
	 
	@Test
	public void test1() {
		orderDao.addOrder();
		orderDao.deleteOrder();
	}
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值