spring1.x AOP实现(也可称spring AOP经典实现),------12月6日在三楼添加了spring2.xAOP实现
环境准备:创建业务逻辑实现:
接口: package testSpringAOP;
public interface BusinessProcess {
public void someMethod();
public void anotherMethod();
}
实现类:package testSpringAOP;
Public class BusinessProcessImpl implements BusinessProcess{
public void someMethod(){
System.out.println("这是类"+this.getClass() +"中,方法someMethod执行的提示。");
}
@Override
public void anotherMethod() {
System.out.println("这是类"+this.getClass() +"中,方法anotherMethod执行的提示。");
}
}
环境准备好之后,下面开始一步一步实现AOP
第一步,创建切面实现类,该类实现了MethodInterceptor接口
package testSpringAOP;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MyFirstAOP implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation arg0) throws Throwable {
System.out.println("新业务开始执行。");
long startTime = System.nanoTime();
Object returnObject = arg0.proceed();
long endTime = System.nanoTime();
System.out.println("新业务执行结束。");
System.out.println("方法" +arg0.getMethod().getName() + "总共执行了"+ (endTime-startTime) + "纳秒。");
return returnObject;
}
}
第二步,配置spring的配置文件,配置bean,加入点(jionpoint),切点(pointcut),织入(weave)等
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!--http://www.springframework.org/dtd/ - Application context definition for Petclinic on Hibernate.-->
<beans>
<bean id="businessProcess" class="testSpringAOP.SomeClass" />
<bean id="updateBusinessProcess" class="testSpringAOP.MyFirstAOP"></bean>
<bean id="pointCut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
<property name="pattern" value=".*someMethod"></property>
</bean>
<bean name="advisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="advice" ref="updateBusinessProcess"></property>
<property name="pointcut" ref="pointCut"></property>
</bean>
<bean id="newBusinessProcess" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="businessProcess"></property>
<property name="interceptorNames">
<list>
<value>advisor</value>
</list>
</property>
</bean>
</beans>
第三步,编写测试类,检验AOP功能:
package testSpringAOP;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class TestClassesAOP implements MethodInterceptor{
public Object invoke(MethodInvocation mi) throws Throwable {
// 前拦截
long begin=System.nanoTime();
// 目标方法执行
Object res=mi.proceed();
// 后拦截
long end=System.nanoTime();
// 获得和目标方法相关信息,都要通过mi
String name=mi.getMethod().getName();
System.out.println("----------------"+name+"执行了"+(end-begin)+"ns");
// 记得把返回值交给调用者
return res;
}
spring的AOP功能对于本人来讲理解起来比较困难,尤其是配置文件的写法。目前还有许多公司在用spring1.x框架,所以这两天花时间做一做实例,重要不在如何使用springAOP,而是学习spring框架的设计思想和巧妙的实现手段。
[ 本帖最后由 西鄙人 于 2010-12-6 11:05 编辑 ]
搜索更多相关主题的帖子:
void spring interface package public
----------------解决方案--------------------------------------------------------
好帖, 顶了, 帮你移到J2EE版吧
----------------解决方案--------------------------------------------------------
spring2.0之后开始支持基于schema命名空间的AOP配置,相对spring1.x配置简单了许多。
还是一楼的例子,用spring2.x配置文件实现:
业务逻辑不做任何改变,照搬过来:
接口: package testSpringAOP;
public interface BusinessProcess {
public void someMethod();
public void anotherMethod();
}
实现类:package testSpringAOP;
Public class BusinessProcessImpl implements BusinessProcess{
public void someMethod(){
System.out.println("这是类"+this.getClass()+"中,方法someMethod执行的提示。");
}
@Override
public void anotherMethod() {
System.out.println("这是类"+this.getClass()+"中,方法anotherMethod执行的提示。");
}
}
新添加spring2.x的切面类,在这里spring2.xAOP的优势就显示出来了,切面就是普通Java类(洋一点的叫法是:POJO),没有spring污染的痕迹:
package testSpringAOP.spring2;
public class MySpring2Aspect {
public void newAddMethod(){
System.out.println("在原有业务逻辑前添加了新功能。");
}
}
在spring2.x的配置文件中添加命名空间aop,简化aop的配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
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-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="oldBusinessProcess" class="testSpringAOP.spring2.BusinessProcessImpl"/>
<bean id="myAspect" class="testSpringAOP.spring2.MySpring2Aspect"/>
<aop:config>
<aop:pointcut id="addBusinessProcess" expression="within(testSpringAOP.spring2.BusinessProcessImpl)"/>
<aop:aspect ref="myAspect" >
<aop:before pointcut-ref="addBusinessProcess" method="newAddMethod"/>
</aop:aspect>
</aop:config>
</beans>
接下来编写测试类:
package testSpringAOP.spring2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring2AOP {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("testSpringAOP/spring2/spring2AOPContext.xml");
BusinessProcess bp=(BusinessProcess)context.getBean("oldBusinessProcess");
bp.someMethod();
bp.anotherMethod();
System.out.println("程序执行完毕。");
}
}
最后,在测试程序时,老出现错误。基本排错技巧是:如果是配置文件分析出错或工厂类出错,可能缺失jar包。如果某个用户类出错,可能类名写错了。
----------------解决方案--------------------------------------------------------