Spring - 8 AspectJ的XML方式完成AOP开发的入门

本文详细解析了AOP(面向切面编程)的关键术语,如Joinpoint、Pointcut、Advice等,并通过AspectJ的XML配置方式,演示了如何在Spring框架中实现AOP,包括创建项目、配置文件、定义切面类及测试类,为读者提供了全面的AOP实践指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

  • 技术分析之AOP的相关术语

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

1.创建JavaWEB项目,引入具体的开发的jar包

    AOP需要的包比之前多了

    com.springsource.org.aopalliance-1.0.0.jar

    com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

 

2.创建Spring的配置文件,引入具体的AOP的schema约束    

注意没网没提示的需要自己导入一下schema


<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">

</beans>

 

3.创建包结构,编写具体的接口和实现类


 

4. 将目标类配置到Spring中

customerDaoImpl

package demo;

public class CustomerDaoImpl implements CustomerDao{

	@Override
	public void demo1() {
		System.out.println(this.getClass().getName() + "." + new Exception().getStackTrace()[0].getMethodName());
	}
	
}
	<!-- 需spring管理类 -->			
	<bean id="customerDao" class="demo.CustomerDaoImpl"/>

5. 定义切面类并配置到spring中

切面类AOPXML

package demo;

/**
 * 定义AOP切面测试类
 * @author Administrator
 *
 */
public class AOPXML {
	
	public void demo1() {
		System.out.println("AOP测试切面增强类");
	}
	
}
	<!-- 定义切面类 -->				
	<bean id="AOPXML" class="demo.AOPXML"/>

6.在配置文件中完成aop的配置

<!-- 配置AOP -->		
	<aop:config>
		<!-- 引入切面类 切入点+通知类型-->
		<aop:aspect ref="AOPXML">
			<!-- 
				配置前置通知,方法运行前增强。
			 	pointcut需要写入切入点表达式
			 	表达式意思为,执行到demo.CustomerDaoImpl.demo1方法之前执行增强类
			-->
			<aop:before method="demo1" pointcut="execution(public void demo.CustomerDaoImpl.demo1())"/>
		</aop:aspect>

完整的applicationContext.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">
				
	<!-- 需spring管理类 -->			
	<bean id="customerDao" class="demo.CustomerDaoImpl"/>
				
	<!-- 定义切面类 -->				
	<bean id="AOPXML" class="demo.AOPXML"/>
	
	<!-- 配置AOP -->		
	<aop:config>
		<!-- 引入切面类 切入点+通知类型-->
		<aop:aspect ref="AOPXML">
			<!-- 
				配置前置通知,方法运行前增强。
			 	pointcut需要写入切入点表达式
			 	表达式意思为,执行到demo.CustomerDaoImpl.demo1方法之前执行增强类
			-->
			<aop:before method="demo1" pointcut="execution(public void demo.CustomerDaoImpl.demo1())"/>
		</aop:aspect>
	
	</aop:config>
				
</beans>

7.编写测试类

package demo;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


/**
 * 依然通过整合junit4来实现测试类
 * @author Administrator
 *
 */

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AOPDemo {
	
	//注解的方式获得bean,无需提供setter方法
	@Resource(name="customerDao")
	private CustomerDao cdao; 
	
	@Test
	public void run1() {
		
		cdao.demo1();
	}
}

 

结果输出:

已在目标类之前实现增强。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值