Spring框架中成功吸引人的一点就是容器事务的管理,提供了一个轻量级的容器事务处理,针对的对象是普通的java类,使用Spring事务管理的话 ,你可以按照自己的业务把一些相关的方法纳入其事务管理里面,这就避免了程序员在处理事务的过程中繁琐的工作.同时这些也是ejb2.X规范里 面吸引人的一点,这在spring里面都很好的提供.虽然在跨容器的事务管理,spring里面并没有提供,但是对于一般的web程序来说,也不需要仅仅 为了那些功能而不得不使用ejb.不过,最近jboss的嵌入式的ejb容器也可以做的更小了,也是开源中的选择之一.无论技术是怎样发展的,当前,我 们先来研究其中AOP实现的方法.
事实上,Spring中的事务处理是通过AOP思想来实现的,Spring AOP与Aspect J和JBoss具有很 大的不同,首先,使用Spring AOP框架的用户要记住的一点是,Spring AOP针对的是方法层次上的实现,而其他两者对字段也提供了支持.说到 Spring AOP的内幕,其实也不难,对于有接口的类,使用的是Java内部类提供的Proxy;而对于那些不实现接口的类,使用的是cglib库,动态创建一 个子类来实现.
在Spring AOP中提供了4种处理切入类型:around,before,after,introduction.顾名思义,
1)around是针对具体的某个切入点的方法(比如,现在有个OrderBook方法,around的切入类型是就这个方法的内部调用,是通过java的元数据,在 运行时通过Method.invoke来调用,具有返回值,当发生意外的时候会终止.记住的一点是,返回值.);
2)before是在方法调用前 调用(在OrderBook方法前调用,但是没有返回值,同时在通常意外情况下,会继续运行下一步方法.记住的一点是没有返回值);
3)after和before刚好相反,没有什么特别的地方.
4)introduction是一个更加特殊的,但功能更加强大的切入类型.比如(你现 在有Book对象,Computer对象,还有几十个这种业务对象,现在你希望在每个这样的对象中都加入一个记录最后修改的时间.但是你又不希望对每 个类都进行修改,因为太麻烦了,同时更重要的一点,破坏了对象的完整性,说不定你以后又不需要这个时间数据了呢...这时怎么办呢?Spring AOP就为你专门实现这种思想提供了一个切入处理,那就是introduction.introduction可以为你动态加入某些方法,这样可以在运行时,强制转换 这些对象,进行插入时间数据的动作,更深的内幕就是C++虚函数中的vtable思想).不过这种动态是以性能作为代价的,使用之前要慎重考虑,这里 我们谈的是技术,所以就认为他是必需的.
程序包图结构如下图:
下面先将代码贴出再说:
Hello.java
/** *//**
* BeforeAdviceSample
*/
package com.fhway.spring.aop;
/** *//**
* @author fuhw
* 2006-10-5 下午12:17:40
*/
public interface Hello ...{
public void sayHello(String name);
}
HelloImpl.java
/** *//**
* BeforeAdviceSample
*/
package com.fhway.spring.aop;
/** *//**
* @author fuhw 2006-10-5 下午01:07:03
*/
public class HelloImpl implements Hello ...{
public void sayHello(String name) ...{
System.out.println("Hello, the world.Your name is " + name);
}
}
TestAfterAdvice.java
/** *//**
* BeforeAdviceSample
*/
package com.fhway.spring.aop;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
/** *//**
* @author fuhw
* 2006-10-5 下午01:31:06
*/
public class TestAfterAdvice implements AfterReturningAdvice...{
public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable ...{
// TODO Auto-generated method stub
System.out.println("Thank you,Come again! ");
}
}
TestBeforeAdvice.java
/** *//**
* BeforeAdviceSample
*/
package com.fhway.spring.aop;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
/** *//**
* @author fuhw
* 2006-10-5 下午01:14:06
*/
public class TestBeforeAdvice implements MethodBeforeAdvice...{
public void before(Method method,Object[] args,Object target) throws Throwable...{
System.out.println("Hi,this is "+this.getClass().getName());
}
}
Main.java
/** *//**
* BeforeAdviceSample
*/
package com.fhway.spring.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
/** *//**
* @author fuhw 2006-10-5 下午01:16:37
*/
public class Main ...{
public static void main(String[] args) throws Exception ...{
ApplicationContext ctx = new FileSystemXmlApplicationContext("applicationContext.xml");
Hello h = (Hello) ctx.getBean("hello");
h.sayHello("fhway");
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="beanTarget" class="com.fhway.spring.aop.HelloImpl"></bean>
<!-- 1.MethodBeforeAdvice -->
<bean id="theBeforeAdvice"
class="com.fhway.spring.aop.TestBeforeAdvice">
</bean>
<!-- 2.AfterReturningAdvice -->
<bean id="theAfterAdvice"
class="com.fhway.spring.aop.TestAfterAdvice">
</bean>
<bean id="theAdvisor"
class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice">
<!--
<ref local="theBeforeAdvice" />
--> <ref local="theAfterAdvice" />
</property>
<property name="mappedName">
<value>say*</value>
</property>
</bean>
<bean id="hello"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.fhway.spring.aop.Hello</value>
</property>
<property name="target">
<ref local="beanTarget" />
</property>
<property name="interceptorNames">
<list>
<value>theAdvisor</value>
</list>
</property>
</bean>
</beans>
1.从代码可以看出Hello.java是target的实现接口;而HelloImpl.java是其实现;
2.TestAfterAdvice.java为AfterReturningAdvice的AOP实现方式;TestBeforeAdvice.java为MethodBeforeAdvice的实现方式;Main.java为测试方法;OK?
3.关于applicationContext.xml应该是我们要谈的重点;"org.springframework.aop.framework.ProxyFactoryBean"有几个属性"proxyInterfaces"(代理接口)"target"(代理目标),"interceptorNames"(目标的通知bean名称),其他属性我将在下一篇文章中介绍;
4.<!-- 1.MethodBeforeAdvice -->&<!-- 2.AfterReturningAdvice -->是可选的切入类型,在测试的时候我们任选其一就可以了,在通知置入点的配置bean "theAdvisor"的23-25的代码就可以对这个类型进行切换的;
分享到:
相关推荐
spring aop jar 包
描述一下Spring AOP? 在Spring AOP中关注点(concern)和横切关注点(cross-cutting concern)有什么不同? AOP有哪些可用的实现? Spring中有哪些不同的通知类型(advice types)? Spring AOP 代理是什么? 引介...
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 系列文章的源码分析&测试代码 帮助大家学习 Spring 源码,更加了解 Spring 的底层 博客专栏地址:https://blog.csdn.net/wang489687009/category_11269905.html
spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop
一、适合人群 1、具备一定Java编程基础,初级开发者 2、对springboot,mybatis,mysql有基本认识 3、对spring aop认识模糊的,不清楚如何实现Java 自定义注解的 ...4、spring boot,mybatis,druid,spring aop的使用
死磕Spring之AOP篇 - Spring AOP两种代理对象的拦截处理(csdn)————程序
基于注解实现SpringAop基于注解实现SpringAop基于注解实现SpringAop
Spring框架的关键组件之一是面向方面编程(AOP)框架。 面向方面的编程需要将程序逻辑分解成不同的部分。 此教程将通过简单实用的方法来学习Spring框架提供的AOP/面向方面编程。
springaop依赖的jar包,spring版本2.5.6,如果需要,可以下载使用,欢迎各位评论指出不足
AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析...
spring aop切面拦截指定类和方法实现流程日志跟踪 一般情况下,在不侵入业务代码的情况下,需要做流程日志跟踪是比较合理的 采用springaop切面思想
AOP的意思就是面向切面编程。本文主要是通过梳理JDK中自带的反射机制,实现 AOP动态代理模式,这也是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(动态代理),包括jdk动态代理和CGLib动态代理
Spring AOP 几个不同使用方法的完整例子,使用Junit4c测试, 在我的博客上有不同配置组合的说明,可以参考
spring aop的demo spring aop的demo
Spring源码最难问题:当Spring AOP遇上循环依赖.docx
NULL 博文链接:https://306963591.iteye.com/blog/1129837
springAOP所需jar包 springAOP所需jar包 springAOP所需jar包 springAOP所需jar包 springAOP所需jar包