`
yangzisai
  • 浏览: 85736 次
  • 性别: Icon_minigender_1
  • 来自: 东莞
社区版块
存档分类
最新评论

spring使用CGLIB代理

阅读更多
Java代码
package com.bjsxt.spring;   
  
public class UserManagerImpl {   
       
       
    public void addUser(String name, String password) {   
        System.out.println("UserManagerImpl.addUser() -- name: " + name);   
    }   
  
    public void delUser(int id) {   
        System.out.println("UserManagerImpl.delUser() -- id: " + id);   
    }   
  
    public void modifyUser(int id, String name, String password) {   
        System.out.println("UserManagerImpl.modifyUser() -- id: " + id);   
    }   
  
}  
package com.bjsxt.spring; public class UserManagerImpl { public void addUser(String name, String password) { System.out.println("UserManagerImpl.addUser() -- name: " + name); } public void delUser(int id) { System.out.println("UserManagerImpl.delUser() -- id: " + id); } public void modifyUser(int id, String name, String password) { System.out.println("UserManagerImpl.modifyUser() -- id: " + id); } }




Java代码
package com.bjsxt.spring;   
  
import org.aspectj.lang.JoinPoint;   
  
public class MySecurityManagerImpl {   
       
    public void checkSecurity(JoinPoint joinPoint) {   
        Object[] args = joinPoint.getArgs();   
        if (args != null) {   
            for (int i = 0; i < args.length; i++) {   
                System.out.println(args[i]);   
            }   
        if ("张三".equals(args[0])) {   
            System.out.println("你没有权限访问");   
        }   
        }   
        //...    
        //...   
        //System.out.println("进行安全检查!!");   
    }   
}  
package com.bjsxt.spring; import org.aspectj.lang.JoinPoint; public class MySecurityManagerImpl { public void checkSecurity(JoinPoint joinPoint) { Object[] args   joinPoint.getArgs();
                                 


2.看看这次spring配置文件是如何配置的.



Xml代码
<?xml version="1.0" encoding="UTF-8"?>  
  
<!--   
  - Application context definition for JPetStore's business layer.   
  - Contains bean references to the transaction manager and to the DAOs in   
  - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").   
  -->  
<beans xmlns="http://www.springframework.org/schema/beans"  
         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"  
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd   
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd   
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">  
       
    <bean id="userManager" class="com.bjsxt.spring.UserManagerImpl"/>  
       
    <bean id="mySecurityManager" class="com.bjsxt.spring.MySecurityManagerImpl"/>  
       
    <aop:config>  
        <aop:pointcut id="allAddMethod" expression="execution(* add*(..))"/>  
        <aop:aspect id="securityAspect" ref="mySecurityManager">  
            <aop:before pointcut-ref="allAddMethod" method="checkSecurity"/>  
        </aop:aspect>        
    </aop:config>  
           
</beans>  
<?xml version="1.0" encoding="UTF-8"?> <!-- - Application context definition for JPetStore's business layer. - Contains bean references to the transaction manager and to the DAOs in - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation"). --> <beans xmlns="http://www.springframework.org/schema/beans" 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="userManager" class="com.bjsxt.spring.UserManagerImpl"/> <bean id="mySecurityManager" class="com.bjsxt.spring.MySecurityManagerImpl"/> <aop:config> <aop:pointcut id="allAddMethod" expression="execution(* add*(..))"/> <aop:aspect id="securityAspect" ref="mySecurityManager"> <aop:before pointcut-ref="allAddMethod" method="checkSecurity"/> </aop:aspect> </aop:config> </beans>




3.写测试类



Java代码
package com.bjsxt.spring;   
  
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("applicationContext-beans.xml");   
        UserManagerImpl userManager = (UserManagerImpl)factory.getBean("userManager");   
        userManager.addUser("张三", "123");   
//      userManager.delUser(1);   
//      userManager.modifyUser(1, "李四", "abc");   
    }   
}  
package com.bjsxt.spring; 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("applicationContext-beans.xml"); UserManagerImpl userManager = (UserManagerImpl)factory.getBean("userManager"); userManager.addUser("张三", "123"); // userManager.delUser(1); // userManager.modifyUser(1, "李四", "abc"); } }


4.我们看看运行结果:



Java代码
张三   
123  
你没有权限访问   
UserManagerImpl.addUser() -- name: 张三   
张三 123 你没有权限访问 UserManagerImpl.addUser() -- name: 张三 


5. 如果目标类实现了接口,默认采用JDK动态代理来实现AOP
    如果目标类没有实现接口,必须添加CGLIB支持,Spring会自动的在JDK和CGLIB代理之间切换
    如果目标类实现了接口,可以定义让spring强制使用CGLIB代理

    如何强制使用CGLIB代理实现AOP
    将<aop:config>定义为<aop:config proxy-target-class="true">,
    并且要引入CGLIB包:SPRING_HOME\lib\cglib\*.jar
http://blog.sina.com.cn/s/blog_44167fca0100eep6.html
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics