AOP之Hello World

本文通过一个具体实例演示了如何使用Spring AOP实现环绕、前置及后置通知的功能,并展示了如何手动创建代理来应用这些通知。

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

 目标

package com.aop.joinpoint;

public class Greeting {

	public void greet() {
		System.out.print("World");
	}
	
	public String greetWithParam(String param) {
		System.out.println("Param is " + param);
		return param + ", nice to see you!";
	}

}

 

 环绕通知(又叫包围通知)

package com.aop.advice;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class AroundAdvice implements MethodInterceptor {

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		System.out.print("Hello ");
		Object ret = invocation.proceed();
		System.out.println("!");
		return ret;
	}

}

 

前置通知

package com.aop.advice;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class BeforeAdvice implements MethodBeforeAdvice {

	@Override
	public void before(Method method, Object[] arg, Object target) throws Throwable {
		if (arg[0] == null) {
			throw new IllegalArgumentException("arg cannot be null.");
		}
		System.out.println("Before method:" + method.getName() + "(\"" + arg[0] + "\")");
	}

}

 

后置通知

package com.aop.advice;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;

public class AfterAdvice implements AfterReturningAdvice {

	@Override
	public void afterReturning(Object returningValue, Method method, Object[] arg, Object target) throws Throwable {
		System.out.println("After method:" + method.getName() + "(\"" + arg[0] + "\")");
	}

}

 

手动创建代理

package com.aop.demo;

import org.springframework.aop.framework.ProxyFactory;

import com.aop.advice.AfterAdvice;
import com.aop.advice.AroundAdvice;
import com.aop.advice.BeforeAdvice;
import com.aop.joinpoint.Greeting;

public class GreetingDemo {

	public static void main(String[] args) {
		Greeting target = new Greeting();
		AroundAdvice aroundAdvice = new AroundAdvice();
		BeforeAdvice beforeAdvice = new BeforeAdvice();
		AfterAdvice afterAdvice = new AfterAdvice();

		ProxyFactory pf = new ProxyFactory();
		pf.addAdvice(aroundAdvice);
		pf.setTarget(target);

		Greeting proxy = (Greeting)pf.getProxy();

		//不使用代理
		target.greet();
		System.out.println();

		//使用代理(包围通用)
		proxy.greet();

		System.out.println();
		pf.removeAdvice(aroundAdvice);
		pf.addAdvice(beforeAdvice);
		proxy = (Greeting)pf.getProxy();

		//使用代理(前置通用)
		System.out.println(proxy.greetWithParam("Bruce"));

		System.out.println();
		pf.removeAdvice(beforeAdvice);
		pf.addAdvice(afterAdvice);
		proxy = (Greeting)pf.getProxy();

		//使用代理(后置通用)
		System.out.println(proxy.greetWithParam("David"));
	}

}

 

输出

/**Output:
World
Hello World!

Before method:greetWithParam("Bruce")
Param is Bruce
Bruce, nice to see you!

Param is David
After method:greetWithParam("David")
David, nice to see you!
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值