`
无量
  • 浏览: 1134008 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

spring-AOP实现方式(1----基于代理的AOP实现)

阅读更多
Spring的AOP实现方式主要有三种:
1、经典的基于代理的AOP实现
2、自动代理的AOP实现
3、AspectJ的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、定义切入点,用来描述要拦截被代理类中的哪些方法,在applicationContext.xml中进行如下配置
<!-- 3.定义切入点 -->
<bean id="userDaoPointCut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
	<property name="pattern" value=".*User"/>
</bean>

注:拦截被代理类中方法名以User结尾的方法,pattern的值为匹配拦截方法的正则表达式
4、定义通知(通知包括代理类和切入点的匹配规则两个属性),相应配置如下
<!-- 4.定义通知 -->
<bean id="userDaoAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
	<property name="advice" ref="userDaoProxy"/>
	<property name="pointcut" ref="userDaoPointCut"/>
</bean>

5、定义代理工厂,通过代理工厂获取被代理类实现接口的实例
<!-- 5.定义代理工厂 -->
<bean id="userDaoProxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
	<property name="target" ref="userDaoImpl"/>
	<property name="interceptorNames" value="userDaoAdvisor"/>
	<property name="proxyInterfaces" value="com.hank.dao.UserDao"/>
</bean>

6、定义测试类进行测试,这里用junit进行测试
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
	UserDao userDao = (UserDao)ctx.getBean("userDaoProxyFactory");
	userDao.saveUser();

注:这里的AOP实现的动态代理和java的动态代理原理基本差不多,所以具体的被代理类,要实现一个接口,只能通过接口进行代理,不能通过类进行代理

具体实现如下:

代码结构如图:



被代理的具体的类:
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="userDaoPointCut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
		<property name="pattern" value=".*User"/>
	</bean>
	
	<!-- 4.定义通知 -->
	<bean id="userDaoAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
		<property name="advice" ref="userDaoProxy"/>
		<property name="pointcut" ref="userDaoPointCut"/>
	</bean>
	
	<!-- 5.定义代理工厂 -->
	<bean id="userDaoProxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="target" ref="userDaoImpl"/>
		<property name="interceptorNames" value="userDaoAdvisor"/>
		<property name="proxyInterfaces" value="com.hank.dao.UserDao"/>
	</bean>
</beans>


测试类,运用junit进行测试
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hank.dao.UserDao;
import junit.framework.TestCase;

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


输入结果:
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
方法执行之前。。。。。。
保存用户信息。。。。。。
方法执行之后。。。。。
  • 大小: 66.6 KB
分享到:
评论

相关推荐

    spring-aop.jar各个版本

    spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...

    开发工具 spring-aop-4.3.6.RELEASE

    开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE...

    spring-aop-5.2.0.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-5.2.0.RELEASE.jar; 赠送原API文档:spring-aop-5.2.0.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.0.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.0.RELEASE.pom;...

    spring-aop-5.0.8.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-aop-5.0.8.RELEASE.jar; 赠送原API文档:spring-aop-5.0.8.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.8.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.8.RELEASE.pom;...

    spring-aop-5.0.10.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-5.0.10.RELEASE.jar; 赠送原API文档:spring-aop-5.0.10.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.10.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.10.RELEASE....

    spring-aop-5.3.10-API文档-中文版.zip

    赠送jar包:spring-aop-5.3.10.jar; 赠送原API文档:spring-aop-5.3.10-javadoc.jar; 赠送源代码:spring-aop-5.3.10-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.10.pom; 包含翻译后的API文档:spring...

    spring-aop-jar

    aopalliance.jar、spring-aop-4.1.6.RELEASE.jar、spring-aspects-4.1.6.RELEASE.jar

    spring-aop-5.3.12-API文档-中英对照版.zip

    赠送jar包:spring-aop-5.3.12.jar; 赠送原API文档:spring-aop-5.3.12-javadoc.jar; 赠送源代码:spring-aop-5.3.12-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.12.pom; 包含翻译后的API文档:spring...

    spring-aop-3.2.5.RELEASE.jar ;spring-aop-3.2.5.jar

    spring-aop-3.2.5.RELEASE.jar

    spring-aop-5.1.3.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-aop-5.1.3.RELEASE.jar; 赠送原API文档:spring-aop-5.1.3.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.1.3.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.1.3.RELEASE.pom;...

    spring-aop-4.3.20.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-aop-4.3.20.RELEASE.jar 赠送原API文档:spring-aop-4.3.20.RELEASE-javadoc.jar 赠送源代码:spring-aop-4.3.20.RELEASE-sources.jar 包含翻译后的API文档:spring-aop-4.3.20.RELEASE-...

    spring-aop-4.0.4.RELEASE

    spring-aop-4.0.4.RELEASE 的jar包,亲测可用。。。。

    spring-aop.jar

    spring-aop.jarspring-aop.jarspring-aop.jarspring-aop.jarspring-aop.jarspring-aop.jarspring-aop.jarspring-aop.jarspring-aop.jarspring-aop.jar

    spring-aop-5.0.4.RELEASE.jar

    spring-aop-5.0.4.RELEASE.jar。

    spring-aop-3.2.0.RELEASE.jar

    spring-aop-3.2.0.RELEASE.jar,一个Spring中AOP的jar包

    spring-aop-2.0.8.jar

    spring-aop-2.0.8.jar

    spring-aop-5.1.0.RELEASE.jar

    spring-**core**-4.3.6.RELEASE.jar :包含spring框架基本的核心工具类,spring其他组件都要用到这个包里的类,其他组件的基本核心 spring-**beans**-4.3.6.RELEASE.jar:所有应用都要用到的jar包,它包含访问配置...

    spring-aop-5.0.1.RELEASE.jar

    spring-aop-5.0.1.RELEASE.jar

    spring-aop-4.1.4.RELEASE.jar

    java spring AOP 支持添加的jar包,有需要的话可以下哦

    spring-aop-5.2.15.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-5.2.15.RELEASE.jar; 赠送原API文档:spring-aop-5.2.15.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.15.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.15.RELEASE....

Global site tag (gtag.js) - Google Analytics