CGLIB代理
2016-01-08 00:02
169 查看
</pre><pre name="code" class="java">package com.test.aop; public class TestManagerImpl { public void addUser(String name, String password) { System.out.println("TestManagerImpl.addUser() -- name: " + name); } public void delUser(int id) { System.out.println("TestManagerImpl.delUser() -- id: " + id); } public void modifyUser(int id, String name, String password) { System.out.println("TestManagerImpl.modifyUser() -- id: " + id); } }
</pre><pre name="code" class="java">package com.test.aop; import org.aspectj.bridge.SourceLocation; import org.aspectj.lang.JoinPoint; public class TestSecurityManagerImpl { <span style="white-space:pre"> </span> public void checkSecurity(JoinPoint joinPoint) { <span style="white-space:pre"> </span> Object[] args = joinPoint.getArgs(); <span style="white-space:pre"> </span> if (args != null) { <span style="white-space:pre"> </span> for (int i = 0; i < args.length; i++) { <span style="white-space:pre"> </span> System.out.println(args[i]); <span style="white-space:pre"> </span> } <span style="white-space:pre"> </span> } <span style="white-space:pre"> </span> Object target = joinPoint.getTarget(); <span style="white-space:pre"> </span> Class<?> classe = joinPoint.getClass(); <span style="white-space:pre"> </span> Object his = joinPoint.getThis(); <span style="white-space:pre"> </span> org.aspectj.lang.Signature signature = joinPoint.getSignature(); <span style="white-space:pre"> </span> SourceLocation sourceLocation = (SourceLocation) joinPoint.getSourceLocation(); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span> System.out.println(joinPoint.getTarget()); <span style="white-space:pre"> </span> //... <span style="white-space:pre"> </span> //... <span style="white-space:pre"> </span> //System.out.println("进行安全检查!!"); <span style="white-space:pre"> </span> } }
package com.test.aop; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import junit.framework.TestCase; public class TestAop extends TestCase { public void testAop1() { //读取配置文件,获取BeanFactory BeanFactory factory = new ClassPathXmlApplicationContext("app/config/applicationContext.xml"); TestManagerImpl userManager = (TestManagerImpl)factory.getBean("testManager"); userManager.addUser("张三", "123"); userManager.delUser(1); userManager.modifyUser(1, "李四", "abc"); } }
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- 启用spring mvc 注解 --> <context:annotation-config /> <!-- 配置使Spring采用CGLIB代理 --> <aop:aspectj-autoproxy proxy-target-class="true" /> <bean id="testManager" class="com.test.aop.TestManagerImpl"></bean> <bean id="testSecurityManager" class="com.test.aop.TestSecurityManagerImpl"></bean> <aop:config> <aop:pointcut id="allAddMethod" expression="execution(* add*(..))"/> <aop:aspect id="securityAspect" ref="testSecurityManager"> <aop:after pointcut-ref="allAddMethod" method="checkSecurity"/> </aop:aspect> </aop:config> </beans>
相关文章推荐
- java动态代理(JDK和cglib)
- 基于JDK和CGLib的链式动态代理
- 获得spring的指定目标对象,执行指定方法(JDK动态代理,cglib动态代理,Dubbo-Javassist代理)
- Java的静态代理、动态代理,CGLib的动态代理,使用动态代理基于AOP的AspectJ框架—深入探究
- java动态代理(JDK和cglib)
- Java动态代理(JDK接口代理和Cglib类代理)
- 流程图解Spring Framework(八) Spring 如何创建代理的--cglib?
- Java设计模式(七) Spring AOP JDK动态代理 VS. cglib
- cglib 与 JDK动态代理的运行性能比较
- cglib动态代理介绍(一)
- 代理模式(静态代理、JDK动态代理原理分析、CGLIB动态代理)
- cglib动态代理
- CGLIb代理
- CGLIB动态代理应用-java使用记录操作日志
- 使用cglib形式创建动态代理
- 动态代理之JDK与CGLib
- 使用CGLib实现动态代理
- cglib动态代理举例
- java动态代理(JDK和cglib)
- tomcat源码分析3—>cglib动态代理深度解析