Spring的AOP实现方式----二:

自动代理的AOP实现

代码和上一篇基于代理的AOP实现是一样的。只是配置文件和测试类有点变化

经典的基于代理的AOP实现,思路整理如下:

1、创建具体的实现类(被代理类bean),同时要在applicationContext.xml中进行相应配置

<!-- 1.定义被代理类bean -->
<bean id="userDaoImpl" class="com.hank.dao.impl.UserDaoImpl"/>

2、创建代理类,根据需要实现对应的如下接口中的一种或几种,同时也要在applicationContext.xml中进行相应配置

Spring支持五种类型的通知:

1、Before(前)org.apringframework.aop.MethodBeforeAdvice

2、After-returning(返回后)org.springframework.aop.AfterReturningAdvice

3、After-throwing(抛出后)org.springframework.aop.ThrowsAdvice

4、Arround(周围)org.aopaliance.intercept.MethodInterceptor

5、Introduction(引入)org.springframework.aop.IntroductionInterceptor

<!-- 2.定义代理类 -->
<bean id="userDaoProxy" class="com.hank.proxy.UserDaoProxy"/>

3、定义正则表达式的通知

4、定义一个表示声明使用自动代理的类

注:这里的AOP实现的动态代理和java的动态代理原理基本差不多,所以具体的被代理类,要实现一个接口,只能通过接口进行代理,不能通过类进行代理;注意测试类中的获取bean和基于代理的AOP实现获取方式不同[/b][/color]

具体实现如下:

代码结构如图:

被代理的具体的类:

package com.hank.dao.impl;
import com.hank.dao.UserDao;

public class UserDaoImpl implements UserDao{
	public void saveUser() {
		System.out.println("保存用户信息。。。。。。");
	}

	public void queryUser() {
		System.out.println("查看用户信息。。。。。。");
	}
}

被代理类实现的接口类

package com.hank.dao;

public interface UserDao {
	//保存用户信息
	public void saveUser();
	//查看用户信息
	public void queryUser();
}

代理类,即拦截处理类

package com.hank.proxy;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

public class UserDaoProxy implements MethodBeforeAdvice, AfterReturningAdvice{

	public void afterReturning(Object arg0, Method arg1, Object[] arg2,
			Object arg3) throws Throwable {
		System.out.println("方法执行之后。。。。。");
	}

	public void before(Method method, Object[] args, Object target)
			throws Throwable {
		System.out.println("方法执行之前。。。。。。");
	}
}

配置文件

<?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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<!-- 1.定义具体被代理类 -->
	<bean id="userDaoImpl" class="com.hank.dao.impl.UserDaoImpl"/>
	 
	<!-- 2.定义代理类 -->
	<bean id="userDaoProxy" class="com.hank.proxy.UserDaoProxy"/>

	<!-- 3.定义支持正则表达式的通知 -->
	<bean id="userDaoAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
		<property name="advice" ref="userDaoProxy"></property>
		<property name="pattern" value=".*User"></property>
	</bean>
	
	<!-- 4.定义一个表示声明使用自动代理的类 -->
	<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
</beans>

测试类,运用junit进行测试

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hank.dao.UserDao;
import junit.framework.TestCase;

public class TestAopByAutoProxy extends TestCase{
	public void test1() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserDao userDao = (UserDao)ctx.getBean("userDaoImpl");
		userDao.saveUser();
	}
}

输入结果:

log4j:WARNNoappenderscouldbefoundforlogger(org.springframework.context.support.ClassPathXmlApplicationContext).

log4j:WARNPleaseinitializethelog4jsystemproperly.

方法执行之前。。。。。。

保存用户信息。。。。。。

方法执行之后。。。。。