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
分享到:
相关推荐
NULL 博文链接:https://lochen514.iteye.com/blog/1271194
Spring框架的AOP中重要的一个知识点,动态代理,springAOP框架会根绝实际情况选择使用jdk的动态代理还是cglib的动态代理
Spring AOP、动态代理中的CGLib代理依赖的所有jar包
cglib代理, 1.cglib(Code Generation Library)是一个强大的,高性能,高质量的Code生成类库。它可以在运行期扩展Java类与实现Java接口。 2.cglib封装了asm,可以在运行期动态生成新的class。 3.cglib用于AOP,...
spring框架已经帮我们整合好了所有cglib要用到的jar包,所以下载这个jar包就可以了,然后使用cglib的代码和导入cglib包以及其依赖包是一模一样的(改一下导包就ok了)
Spring AOP 使用的动态代理主要有两种方式:JDK 动态代理和 CGLIB 代理。 JDK 动态代理:用于代理实现了接口的类。Spring 会使用 java.lang.reflect.Proxy 类来创建代理对象。 CGLIB 代理:用于代理没有实现接口的...
CGLIB代理例子
springAOP等框架中,大量用到代理技术; 两类代理技术: 1,java动态代理,见JDK文档或相关java书; 2.cglib创建代理类,比第一种更加简单,代理类和背代理类不需要实现共同接口。本例子就是cglib代理的例子,很简单
主要介绍了Spring如何基于Proxy及cglib实现动态代理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
NULL 博文链接:https://quicker.iteye.com/blog/673889
spring之AOP(动态代理),包括jdk动态代理和CGLib动态代理
使用mindmaster打开
如果想使用CGLIB的技术来生成代理对象,那么需要引入CGLIB的开发的jar包,在Spring框架核心包中已经引入了CGLIB的开发包了。所以直接引入Spring核心开发包即可!
Spring框架系列(11) - Spring AOP实现原理详解之Cglib代理实现.doc
cglib实现动态代理所依赖的jar包 在学习spring中会用到
Spring可以单独应用于构筑应用程序,也可以和Struts、Webwork等众多Web框架组合使用,并且可以与Swing等桌面英语程序AP组合。Spring不仅能应用于J2EE应用程序中,也可以用于桌面应用程序以及小应用程序之中。 Spring...
spring-demo20-CGLIB动态代理.zip
cglib动态代理相关jar包,亲测可用cglib动态代理相关jar包,亲测可用cglib动态代理相关jar包,亲测可用
它被广泛使用在基于代理的AOP框架(例如Spring AOP和dynaop)提供方法拦截。Hibernate作为最流行的ORM工具也同样使用CGLIB库来代理单端关联(集合懒加载除外,它使用另外一种机制)。EasyMock和jMock作为流行的Java...
Spring-AOP-利用java中的动态代理和Spring的拦截器做到AOP