基于JDK动态代理的经典Spring AOP

我们知道,要使用JDK的动态代理,目标类需要实现至少一个接口,下面定义了一个接口:
package com.zzj.aop;

public interface Animal {
	public void eat();
}
目标类:
package com.zzj.aop;

public class Human implements Animal {

	@Override
	public void eat() {
		System.out.println("eat...");
	}

}
前置通知:
package com.zzj.aop;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class MethodBefore implements MethodBeforeAdvice {

	public void before(Method arg0, Object[] arg1, Object arg2)
			throws Throwable {
		System.out.println("before...");
	}

}
后置通知:
package com.zzj.aop;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;


public class MethodAfter implements AfterReturningAdvice {

	public void afterReturning(Object arg0, Method arg1, Object[] arg2,
			Object arg3) throws Throwable {
		System.out.println("after...");
	}

}
spring配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">
       <!-- 定义目标对象 -->
       <bean id="human" class="com.zzj.aop.Human"></bean>
       <!-- 定义通知 -->
       <bean id="before" class="com.zzj.aop.MethodBefore"></bean>
       <bean id="after" class="com.zzj.aop.MethodAfter"></bean>
       <!-- 定义代理对象 -->
       <bean id="humanProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
       		<!-- 指定代理接口 -->
       		<property name="interfaces">
       			<list>
       				<value>com.zzj.aop.Animal</value>
       			</list>
       		</property>
       		<!-- 指定目标对象  -->
       		<property name="target" ref="human"></property>
       		<!-- 指定拦截器(通知) -->
       		<property name="interceptorNames">
       			<list>
       				<value>after</value>
       				<value>before</value>
       			</list>
       		</property>
       </bean>
       
</beans>
测试:
package com.zzj.aop;

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

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		Animal animal = (Animal) context.getBean("humanProxy");
		animal.eat();

	}

}
输出:
before...
eat...
after...
特别注意:只有调用在接口里声明的方法才会经过代理。ProxyFactoryBean为目标对象创建了一个代理对象,并在IOC容器里注册了它。默认情况下,ProxyFactoryBean自动侦测并且代理目标对象所实现的所有接口,所以,如果想代理目标对象的所有接口,可以不必显示地指定这些接口。代理Bean可如下配置:
<bean id="humanProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
       		<!-- 指定目标对象  -->
       		<property name="target" ref="human"></property>
       		<!-- 指定拦截器(通知) -->
       		<property name="interceptorNames">
       			<list>
       				<value>after</value>
       				<value>before</value>
       			</list>
       		</property>
</bean>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值