Spring AOP

本文参考JavaWeb编程实战宝典

AOP解释

AOP——面向方面编程

OOP(面向对象编程)有时候会存在一定的缺点,比如说有时候我需要监控一个方法的被调用情况,在该方法内加入了特定的功能——使其能够将该方法的被调用情况记录下来,但是这个功能不是该方法所关注的主要业务。当加入了很多该方法所不关注的业务时,就会使得该方法“变味”了。为了将方法所关注的主要业务和次要业务区分开,使程序代码变得不那么臃肿,AOP诞生了。


AOP术语

Aspect:Aspect就是方法中非主要的业务逻辑(如上面所说的监控方法的被调用情况)

Advice:把主要业务逻辑与非主要业务逻辑分开,将非主要业务逻辑写入到另外一个类中。

Target:Target就是AOP所要拦截的目标类,也即该方法所属的类

Proxy:需要实现目标接口,AOP在拦截时通过代理对象来访问Target,从而实现在Target方法中插入非主要业务逻辑的目的


Advice

Spring AOP中有4中类型的Advice,分别是Before Advice、After Advice、Around Advice、Throws Advice,现在我以Before Advice进行说明

Before Advice

第一步编写Target接口

public interface MyInterface {
	public String getHello(String name);
	public int getRandomInt(int max);
}


实现该目标类

public class MyClass implements MyInterface {

	@Override
	public String getHello(String name) {
		// TODO Auto-generated method stub
		System.out.println("调用getHello方法");
		return "Hello "+name;
	}

	@Override
	public int getRandomInt(int max) {
		// TODO Auto-generated method stub
		Random random = new Random();
		System.out.println("调用getRandomInt方法");
		return random.nextInt(max);
	}

}

第二步编写Before Advice类,该类需要实现Spring中的MethodBeforeAdvice接口

public class BeforeAdvice implements MethodBeforeAdvice {

	@Override
	public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
		// TODO Auto-generated method stub
		System.out.println("before advice:"+arg2.getClass().getName()+"."+arg0.getName()+",方法参数值是:"+arg1[0]);
		if(arg0.getName().equals("getHello")){
			arg1[0]="东方不败";
		}
	}

}

在Before Advice中重写了before方法:

void before(Method method, Object[] args, Object target) throws Throwable;

其中:

method:表示前面所说的方法

args:表示该方法的参数值

target:表示该方法所在的类的对象


第三部建立Proxy类

<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="proxyInterfaces">
			<list>
				<value>com.aop.before.advice.MyInterface</value>
			</list>
		</property>
		<property name="interceptorNames">
			<list>
				<value>beforeAdvice</value>
			</list>
		</property>
		<property name="target">
			<ref bean="myClass"/>
		</property>
	</bean>

其中

proxyInterfaces:表示代理所实现的接口,注意一点,未在给接口中定义的方法Spring AOP是无法拦截的

interceptorNames:表示拦截此方法的拦截器名

target:需要拦截的目标类

在这里实际上使用到了设计模式中的代理模式。从下面的Client类中,也是可以看出,在Client类中,我们看似调用的是实现MyInterface接口的ProxyFactoryBean中的方法,而实际上它不过是一个代理对象,实际上的对象是实现MyInterface接口的MyClass对象。


于此同时还需要配置目标类和Before Advice类

<bean id="myClass" class="com.aop.before.advice.MyClass">
	</bean>
	
	<bean id="beforeAdvice" class="com.aop.before.advice.BeforeAdvice"></bean>

下面进行测试:

public class Client {
	public static void main(String[] args) {
		ApplicationContext context = new FileSystemXmlApplicationContext("./src/com/aop/before/advice/aop.xml");
		MyInterface myInterface = (MyInterface) context.getBean("proxy");
		System.out.println(myInterface.getHello("令狐冲"));
		System.out.println(myInterface.getRandomInt(30));
	}
}

显示结果如下所示:


总结

Spring AOP非常好地避免了代码的臃肿,不仅如此,我们可以看到AOP还是很好的利用了IOC技术,提高了代码的可维护性,大大降低了类与类之间的耦合度。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值