SpringAOP的简单示例(xml配置方式)

本文通过一个简单的示例介绍了如何使用XML配置实现Spring AOP。首先阐述了Spring AOP与AspectJ的关系,指出Spring AOP侧重于与Spring IOC容器的结合,采用动态代理实现。接着,详细讲解了从基础jar包准备到UserService接口、UserServiceImpl实现、切面类编写,再到applicationContext.xml的配置过程,最后展示了运行结果,证实了save和update方法被成功拦截。

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

1.SpringAOP的基础jar包

在这里插入图片描述
其中:

aopalliance.jar
aspectjrt.jar
aspectjweaver.jar
这三个包不属于Spring自身jar包,他们属于AspectJ技术

简单介绍Spring与AspectJ的关系:

Spring AOP 与ApectJ 的目的一致,都是为了统一处理横切业务,但与AspectJ不同的是,Spring AOP 并不尝试提供完整的AOP功能(即使它完全可以实现),Spring AOP 更注重的是与Spring IOC容器的结合,并结合该优势来解决横切业务的问题,因此在AOP的功能完善方面,相对来说AspectJ具有更大的优势,同时,Spring注意到AspectJ在AOP的实现方式上依赖于特殊编译器(ajc编译器),因此Spring很机智回避了这点,转向采用动态代理技术的实现原理来构建Spring AOP的内部机制(动态织入),这是与AspectJ(静态织入)最根本的区别。在AspectJ 1.5后,引入@Aspect形式的注解风格的开发,Spring也非常快地跟进了这种方式,因此Spring 2.0后便使用了与AspectJ一样的注解。请注意,Spring 只是使用了与 AspectJ 5 一样的注解,但仍然没有使用 AspectJ 的编译器,底层依是动态代理技术的实现,因此并不依赖于 AspectJ 的编译器。

2.编写UserService的接口

package com.zslaa.service;

public interface UserService {

	public void save();
	
	public void update();
}

3.编写UserServiceImpl的实现

package com.zslaa.service.impl;

import com.zslaa.service.UserService;
/**
 * 这个类在AOP属于目标对象(Target)
 */
public class UserServiceImpl implements UserService {

	@Override
	public void save() {
		System.out.println("执行save方法");
	}

	@Override
	public void update() {
		System.out.println("执行update方法");
	}

}

4.编写切面类

package com.zslaa.aspect;

/**
 * Spring的AOP的切面类
 */
public class MyAspect {

	/**
	 * 通知方法
	 */
	public void writeLog(){
		System.out.println("使用spring的AOP切入日志...");
	}

}

5.配置applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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">

	<!-- 基于AspectJ的spring的AOP编写之XML方式 -->
	
	<!-- 1.创建目标对象 -->
	<bean id="userService" class="com.zslaa.service.impl.UserServiceImpl"/>
	
	<!-- 2.创建切面类对象 -->
	<bean id="myAspect" class="com.zslaa.aspect.MyAspect"/>
	
	<!-- 3.配置AOP切面 -->
	<aop:config>
		<!-- 切面 = 通知+切入点 -->
		<aop:aspect ref="myAspect">
			<!-- 
				aop:before: 代表前置通知(后面会详细介绍各种不同类型的通知的用法)
				writeLog: 该方法为MyAspect类中的writeLog方法
				pointcut-ref: 代表引用一个切入点
			 -->
			<aop:before method="writeLog" pointcut-ref="pt"/>
			<!-- 
				aop:pointcut:代表切入点配置
				expression:这里配置切入点表达式,用于声明哪些方法需要被拦截
			 -->
			<aop:pointcut expression="execution(public void com.zslaa.service.impl.UserServiceImpl.*())" id="pt"/>
		</aop:aspect>
	</aop:config>
	
</beans>

6.运行结果

在这里插入图片描述
总结:可以看到save和update方法成功被SpringAOP拦截。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值