欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring aop 原理 测试 博客分类: spring AOPSpringBeanXML 

程序员文章站 2024-03-04 18:38:06
...
项目中要使用Spring aop进行权限方面的验证. 正好研究下简单的aop原理..使用的是spring 3.0
---------
接口
package com.iknowing.springaop.test;

public interface Bean {
public void theMethod();
}

-----------
接口实现
package com.iknowing.springaop.test;

public class BeanImpl implements Bean {

@Override
public void theMethod() {
System.out.println(this.getClass().getName()
     + "." + new Exception().getStackTrace()[0].getMethodName()

     + "()"

     + " says HELLO!");
}

}
-----------
前置拦截
package com.iknowing.springaop.test;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class TestBeforeAdvice implements MethodBeforeAdvice {

public void before(Method m, Object[] args, Object target)throws Throwable {
   System.out.println("Hello world! (by " + this.getClass().getName()+ ")");
}

}


---------------------
测试
Test:
package com.iknowing.springaop.test;

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

public class Test {
public static void main(String[] args) {
ApplicationContext ctx= new ClassPathXmlApplicationContext("applicationContext.xml");
Bean x =ctx.getBean("bean",Bean.class);
x.theMethod();
}
}
---------------------
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>

<!--CONFIG-->

<bean id="bean" class="org.springframework.aop.framework.ProxyFactoryBean">

<property name="proxyInterfaces">

    <value>com.iknowing.springaop.test.Bean</value>

   </property>

   <property name="target">

    <ref local="beanTarget"/>

   </property>

   <property name="interceptorNames">

    <list>

     <value>theAdvisor</value>

    </list>

   </property>

</bean>

<!--CLASS-->

<bean id="beanTarget" class="com.iknowing.springaop.test.BeanImpl"/>

<!--ADVISOR-->

<!--Note: An advisor assembles pointcut and advice-->

<bean id="theAdvisor" class="org.springframework.aop.support.RegexpMethod PointcutAdvisor">

   <property name="advice">

    <ref local="theBeforeAdvice"/>

   </property>

   <property name="pattern">

    <value>com\.iknowing\.springaop\.test\.Bean\.theMethod</value>

   </property>

</bean>

<!--ADVICE-->

<bean id="theBeforeAdvice" class="com.ascenttech.springaop.test.TestBefore Advice"/>

</beans>

------------------
自己实现spring aop的动态代理:

JdkDynamicAop:

package com.iknowing.springaop.test;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JdkDynamicAop implements InvocationHandler {

private Object targetObj;

public Object bind(Object obj){
this.targetObj=obj;
return Proxy.newProxyInstance(targetObj.getClass().getClassLoader(),targetObj.getClass().getInterfaces(),this);
}

@Override
public Object invoke(Object proxy, Method method, Object[] parameter)
throws Throwable {
Object value=null;
System.out.println("方法"+method.getName()+"开始调用");
    value=method.invoke(targetObj,parameter);
    System.out.println("方法"+method.getName()+"结束");
return value;
}

}
------
MessageWrite :
package com.iknowing.springaop.test;

public class MessageWrite {
public void write(){
System.out.print("world");
}
}

-------------
调用
JdkDynamicAop da=new JdkDynamicAop();
Bean proxy=(Bean)da.bind(new BeanImpl());
proxy.theMethod();

spring调用:
//在这里使用的是spring 的cglib代理 我没有接口 所有要添加cglibjar和asmjar
                  MessageWrite target=new MessageWrite();
ProxyFactory pf=new ProxyFactory(); //创建工厂类
pf.addAdvice(new MethodAroundAdvice()); //注册advice
pf.setTarget(target); //设置目标对象

MessageWrite proxy1=(MessageWrite) pf.getProxy(); //创建动态对象
proxy1.write(); //调用方法

--------------
静态代理

StaticAop:
package com.iknowing.springaop.test;

public class StaticAop implements Bean {

public Bean bean;

public StaticAop(Bean bean){
this.bean=bean;
}

@Override
public void theMethod() {
System.out.println("方法开始调用");
bean.theMethod();
System.out.println("方法调用结束");
}

}

---
                 //静态代理调用
StaticAop sa=new StaticAop(new BeanImpl());
sa.theMethod();