您的位置:首页 > 其它

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>



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: