spring aop 原理 测试

项目中要使用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();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值