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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值