项目中要使用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();
分享到:
相关推荐
SpringAOP入门和原理分析,动态代理
20 Spring Boot AOP 21 Spring Boot Starter18:31 --快速构建自定义的Spring Boot Starter 22 Spring Boot 日志30:58 --演示了如何在Spring Boot里面使用日志配置,以及logback,log4j2等日志的使用 23 Spring ...
02_搭建与测试Spring的开发环境 03_编码剖析Spring管理Bean的原理 04_Spring的三种实例化Bean的方式 05_配置Spring管理的bean的作用域 06_Spring管理的Bean的生命周期 07_编码剖析Spring依赖注入的原理 08_...
第3章 Spring AOP和AspectJ支持 112 3.1 启用Spring的AspectJ注解支持 113 3.1.1 问题 113 3.1.2 解决方案 113 3.1.3 工作原理 113 3.2 用AspectJ注解声明aspect 115 3.2.1 问题 115 3.2.2 解决方案...
让我们在开发中以及开发后期进行测试时,直接使用Junit结合spring一起测试。 本套课程中,我们将全面剖析Spring和SpringMVC两个部分。从应用场景分析,到基本用法的入门案例,再到高级特性的分析及使用,最后是执行...
此外,我们还对Spring技术所涉及到的各种Java技术、Java设计模式进行了适时的介绍,通过这些背景知识的准备,读者在理解Spring的各项原理时将不会有任何的障碍。 =============================================...
第3章 Spring AOP和AspectJ支持 112 3.1 启用Spring的AspectJ注解支持 113 3.1.1 问题 113 3.1.2 解决方案 113 3.1.3 工作原理 113 3.2 用AspectJ注解声明aspect 115 3.2.1 问题 115 3.2.2 解决方案...
使用注解配置实现Spring动态数据源切换,实现原理 1、自定义动态数据源类DynamicDataSource: 实现spring类AbstractRoutingDataSource的方法determineCurrentLookupKey 2、自定义Spring AOP类DataSourceAspect 3、...
6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.7. 编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 ...
6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.7. 编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1....
此外,我们还对Spring技术所涉及到的各种Java技术、Java设计模式进行了适时的介绍,通过这些背景知识的准备,读者在理解Spring的各项原理时将不会有任何的障碍。 =============================================...
此外,我们还对Spring技术所涉及到的各种Java技术、Java设计模式进行了适时的介绍,通过这些背景知识的准备,读者在理解Spring的各项原理时将不会有任何的障碍。 =============================================...
经过历时一年的重大调整改版而成的,本书延续了上一版本追求深度,注重原理,不停留在技术表面的写作风格,力求使读者在熟练使用Spring的各项功能的同时,还能透彻理解Spring的内部实现,真正做到知其然知其所以然。...
6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 6.1.3. Spring的AOP代理 6.2. @AspectJ支持 6.2.1. 启用@AspectJ支持 6.2.2. 声明一个切面 6.2.3. 声明一个...
6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.6.1. 理解AOP代理 6.7. 以编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ进行domain ...
Spring核心功能梳理,配套测试代码。包含:Bean的生命周期、依赖注入的实现、Bean初始化原理、推断构造方法原理、AOP的实现等演示代码。
6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.6.1. 理解AOP代理 6.7. 以编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ进行domain ...
经过历时一年的重大调整改版而成的,本书延续了上一版本追求深度,注重原理,不停留在技术表面的写作风格,力求使读者在熟练使用Spring的各项功能的同时,还能透彻理解Spring的内部实现,真正做到知其然知其所以然。...
12.2.2在Spring MVC测试中以用户身份运行测试 106 12.2.3使用RequestPostProcessor在Spring MVC测试中以用户身份运行 106 作为用户在Spring MVC测试中使用注释运行 108 12.2.4测试HTTP基本认证 109 12.3 ...