使用Spring的注解方式实现AOP入门(1)

本文介绍如何在Spring框架中利用AOP实现方法前后的增强处理。通过示例代码详细展示了如何为特定的方法添加前置和后置通知,包括使用@AspectJ注解、定义切入点表达式等关键步骤。

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

要进行AOP编程,首先要在Spring 的配置文件中引入aop的命名空间

<?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-2.5.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
</beans>

 使用切面编程还需要使用以下的jar文件

lib/aspectj/aspectjjweaver.jar和aspectjrt.jar

lib/cglib/cglib-nodep-2.1-3.jar

启动对@AspectJ注解的支持

<?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-2.5.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
       <aop:aspectj-autoproxy/>
</beans>

 加入业务bean

package servlice;


public interface Person {
	public void eat();
	public void sleep();

}

 

package impl;

import servlice.Person;

public class PersonImpl implements Person{

	public void eat() {
		System.out.println("吃饭了");
		
	}

	public void sleep() {
		System.out.println("睡觉去");
		
	}

}

 

package intercept;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;


@Aspect
public class MyIntercept {
	@Pointcut("execution(* impl.PersonImpl.*(..))")
	private void anyMethod(){}
//	第一个*代表所有的返回类型
//	第二个*代表里面所有的方法
	@Before("anyMethod()")
	private void before()
	{
		System.out.println("吃饭前先洗手");
	}
	
}

 

在xml方法中注册bean

<?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-2.5.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
       <aop:aspectj-autoproxy/>
       <bean id="myIntercept" class="intercept.MyIntercept"></bean>
       <bean id="person" class="impl.PersonImpl"></bean>
</beans>

 测试类

package text;

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

import servlice.Person;

public class AopText {
	public static void main(String[] args) {
	ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
	Person person=(Person) context.getBean("person");
	person.eat();
	person.sleep();
	}
}

 测试结果

吃饭前先洗手
吃饭了
吃饭前先洗手
睡觉去
修改MyIntercept

package intercept;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;


@Aspect
public class MyIntercept {
	@Pointcut("execution(* impl.PersonImpl.eat(..))")//修改了这里
	private void eat(){}
//	第一个*代表所有的返回类型
//	第二个*代表里面所有的方法
	@Before("eat()")
	private void beforeEat()
	{
		System.out.println("吃饭前先洗手");
	}
	@After("eat()")
	private void afterEat()
	{
		System.out.println("饭后漱口");
	}
}

  测试结果

吃饭前先洗手
吃饭了
饭后漱口
睡觉去

 

再次修改

package intercept;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;


@Aspect
public class MyIntercept {
	@Pointcut("execution(* impl.PersonImpl.eat(..))")//修改了这里
	private void eat(){}
//	第一个*代表所有的返回类型
//	eat表示类里面的eat方法
	@Before("eat()")
	private void beforeEat()
	{
		System.out.println("吃饭前先洗手");
	}
	@After("eat()")
	private void afterEat()
	{
		System.out.println("饭后漱口");
	}
	@Pointcut("execution(* impl.PersonImpl.sleep(..))")//添加了这些
	private void sleep(){}
	@Before("sleep()")
	private void beforeSleep()
	{
		System.out.println("睡觉前先刷牙洗脸");
	}
	@After("sleep()")
	private void afterSleep()
	{
		System.out.println("睡觉起来也要刷牙洗脸");
	}
}

  结果

吃饭前先洗手
吃饭了
饭后漱口
睡觉前先刷牙洗脸
睡觉去
睡觉起来也要刷牙洗脸

再次修改

package intercept;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;


@Aspect
public class MyIntercept {
	@Pointcut("execution(* impl.PersonImpl.eat(..))")//修改了这里
	private void eat(){}
//	第一个*代表所有的返回类型
//	eat表示类里面的eat方法
	@Before("eat()")
	private void beforeEat()
	{
		System.out.println("吃饭前先洗手");
	}
	@After("eat()")
	private void afterEat()
	{
		System.out.println("饭后漱口");
	}
	@Pointcut("execution(* impl.PersonImpl.sleep(..))")//添加了这些
	private void sleep(){}
	@Before("sleep()")
	private void beforeSleep()
	{
		System.out.println("睡觉前先刷牙洗脸");
	}
	@AfterReturning("sleep()")
	private void afterSleep()
	{
		System.out.println("睡觉起来也要刷牙洗脸");
	}
	@Around("sleep()")
	private Object around(ProceedingJoinPoint joinPoint) throws Throwable
	{
		System.out.println("准备睡觉");
		Object result=joinPoint.proceed();
		System.out.println("睡着了");
		return result;
	}
}

 运行结果:

吃饭前先洗手
吃饭了
饭后漱口
准备睡觉
睡觉前先刷牙洗脸
睡觉去
睡着了
睡觉起来也要刷牙洗脸

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值