spring:一个简单实例之AOP

本文介绍AOP(面向切面编程)的概念及其如何补充和完善传统的面向对象编程(OOP)。通过一个Spring AOP的实际例子,展示了如何利用AOP技术来管理横切关注点,如日志记录、权限验证等,从而提高代码的可维护性和复用性。

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

AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善。OOP引入封装、继承、多态等概念来建立一种对象层次结构,用于模拟公共行为的一个集合。不过OOP允许开发者定义纵向的关系,但并不适合定义横向的关系,例如日志功能。日志代码往往横向地散布在所有对象层次中,而与它对应的对象的核心功能毫无关系对于其他类型的代码,如安全性、异常处理和透明的持续性也都是如此,这种散布在各处的无关的代码被称为横切(cross cutting),在OOP设计中,它导致了大量代码的重复,而不利于各个模块的重用。

AOP技术恰恰相反,它利用一种称为"横切"的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将其命名为"Aspect",即切面。所谓"切面",简单说就是那些与业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减少系统的重复代码,降低模块之间的耦合度,并有利于未来的可操作性和可维护性。

使用"横切"技术,AOP把软件系统分为两个部分:核心关注点横切关注点。业务处理的主要流程是核心关注点,与之关系不大的部分是横切关注点。横切关注点的一个特点是,他们经常发生在核心关注点的多处,而各处基本相似,比如权限认证、日志、事物。AOP的作用在于分离系统中的各种关注点,将核心关注点和横切关注点分离开来。

一个简单的 spring aop 事例

1、创建 spring 工程(Java project)

2、导入相应 jar 包

3、创建 PrintService 抽象类

package com.java.spring.service;

public interface PrintService {

	public void say(String name);
}

4、实现  PrintService 抽象类

package com.java.spring.service.impl;

import com.java.spring.service.PrintService;

public class PrintServiceImpl implements PrintService{

	@Override
	public void say(String name) {
		System.out.println(name+"说");
//		System.out.println(1/0);
	}

}

5、创建 PrintServiceAspect 类

package com.java.spring.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

public class PrintServiceAspect {
	
	//前置通知
	public void doBefore(JoinPoint jp){
		System.out.println("这是前置通知");
	}

	//后置通知
	public void deAfter(JoinPoint jp){
		System.out.println("这是后置通知");
	}
	
	//环绕通知
	public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
		System.out.println("说话前");
		Object object = pjp.proceed();
		System.out.println("执行结果:"+object);
		System.out.println("说话后");
		return object;
	}
	
	//返回通知
	public void doReturn(JoinPoint jp){
		System.out.println("这是返回通知");
	}
	
	//异常通知
	public void doThrowing(JoinPoint jp,Throwable e){
		System.out.println("这是异常通知");
		System.out.println("异常信息:"+e.getMessage());
	}
}

6、配置 beans.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 id="printServiceAspect" class="com.java.spring.advice.PrintServiceAspect"></bean>
	
	<bean id="printService" class="com.java.spring.service.impl.PrintServiceImpl"></bean>
	
	<aop:config>
		<aop:aspect id="printServiceAspect" ref="printServiceAspect">
			<aop:pointcut expression="execution(* com.java.spring.service.*.*(..))" id="serviceAspect"/>
			<aop:before method="doBefore" pointcut-ref="serviceAspect"/>
			<aop:after method="deAfter" pointcut-ref="serviceAspect"/>
			<aop:around method="doAround" pointcut-ref="serviceAspect"/>
			<aop:after-throwing method="doThrowing" pointcut-ref="serviceAspect" throwing="e"/>
		</aop:aspect>
	</aop:config>
</beans>

7、测试类 PrintTest

package com.java.spring.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.java.spring.service.PrintService;

public class PrintTest {

	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
		PrintService ps = (PrintService)ac.getBean("printService");
		ps.say("张三");
	}
}

到此结束啦~~












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值