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();
---------
接口
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();
推荐阅读
-
spring aop 原理 测试 博客分类: spring AOPSpringBeanXML
-
spring aop 原理 测试 博客分类: spring AOPSpringBeanXML
-
spring3的AOP(备忘) 博客分类: spring spring3aop
-
【Danny hui】运用抽象工厂模式自己动手写一个IoC 博客分类: 专家文章翻译 IOC设计模式单元测试Spring编程
-
java操作Excel、PDF文件 博客分类: java ExcelJavaJ#单元测试Spring
-
spring autowired mockito单元测试 博客分类: 单元测试 springautowiredmockito单元测试
-
A-O-P配置事务 博客分类: Spring AOPSpringBeanXML
-
A-O-P配置事务 博客分类: Spring AOPSpringBeanXML
-
Spring AOP 实现原理与 CGLIB 应用 博客分类: Spring SpringAOPCGLIB
-
spring-aop-aspectj-case 博客分类: spring springaopaspectj