- 浏览: 1242973 次
- 性别:
- 来自: 深圳
-
文章分类
- 全部博客 (718)
- HTML (13)
- JS基础 (23)
- JS应用 (40)
- AJAX (6)
- JSP相关 (12)
- JAVA基础 (52)
- JAVA应用 (74)
- APPLET (11)
- SWING\RCP (2)
- JAVA反射 (6)
- 设计模式 (26)
- 数据库设计 (20)
- Struts (35)
- Struts2 (12)
- Spring (22)
- Hibernate (45)
- Ibatis (18)
- mybatis (3)
- SSH (8)
- UML (5)
- WebService (3)
- XML (16)
- Log4j (7)
- WEB容器 (26)
- 数据结构 (36)
- Linux (34)
- Ruby on Rails (1)
- 其它技术 (27)
- IDE配置 (15)
- 项目实战 (2)
- Oracle (69)
- JAVA报表 (7)
- Android学习 (2)
- 博客链接 (1)
- 网络基础 (1)
- WEB集群 (1)
- .Net开发 (11)
- PB (4)
- 系统构建 (15)
最新评论
-
jnjeC:
牛逼啊哥们,讲得太好了
Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包 -
九尾狐的yi巴:
很好 感谢!
Itext中文处理(更新版) -
luweifeng1983:
有用的,重启一下嘛。
设置eclipse外部修改文件后自动刷新 -
Master-Gao:
设置了也不管用,怎么破呢?
设置eclipse外部修改文件后自动刷新 -
aigo_h:
锋子还有时间写博客,还是很闲哈!
Add directory entries问题
基于schema的aop只是将配置写到配置文件中。
代码:
package com.lwf.aop; public interface UserManager { public void add(String name, String password); public void del(String id); public void modify(int id ,String name, String password); }
package com.lwf.aop; public class UserManagerImpl implements UserManager { public void add(String name, String password) { System.out.println("add method"); } public void del(String id) { System.out.println("del method"); } public void modify(int id, String name, String password) { System.out.println("modify method"); } }
package com.lwf.aop; public interface MySecurityManager { public void checkSecurity(); }
package com.lwf.aop; public class MySecurityManagerImpl implements MySecurityManager { public void checkSecurity() { System.out.println("User security Check"); } }
注意上面的实现类中没有做任何@AspectJ的声明。
配置文件:
<?xml version="1.0" encoding="UTF-8"?> <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.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" > <bean id="userManager" class="com.lwf.aop.UserManagerImpl"></bean> <bean id="mySecurityManager" class="com.lwf.aop.MySecurityManagerImpl"></bean> <aop:config> <aop:aspect id="mySecurity" ref="mySecurityManager"> <aop:pointcut expression="execution(* add*(..)) || execution(* del*(..))" id="allAddMethod" /> <aop:before method="checkSecurity" pointcut-ref="allAddMethod"/> </aop:aspect> </aop:config> </beans>
测试类:
package com.lwf.aop; import junit.framework.TestCase; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client extends TestCase{ public void testAop(){ ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); UserManager userManager = (UserManager)ac.getBean("userManager"); userManager.add("zhangshang", "123456"); userManager.del("23"); } }
结果:
User security Check add method User security Check del method
注意我们配置文件中删除了:<aop:aspectj-autoproxy/>
JoinPoint的使用
在上面的checkSecurity方法中,我们没办法得到横切对象方法如add方法的参数。我们使用JoinPoint来解决。
上面示例只需要修改对应的checkSecurity方法即可。如我们修改MySecurityManagerImpl如下:
package com.lwf.aop; import org.aspectj.lang.JoinPoint; public class MySecurityManagerImpl implements MySecurityManager { 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+"] =" + args[i]); } } System.out.println("User security Check"); } }
我们通过给checkSecurity方法增加JoinPoint joinPoint参数,再通过getArgs方法得到add方法的参数。
现在测试Client.java
package com.lwf.aop; import junit.framework.TestCase; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client extends TestCase{ public void testAop(){ ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); UserManager userManager = (UserManager)ac.getBean("userManager"); userManager.add("zhangshang", "123456"); //userManager.del("23"); } }
输出如下:
args[0] =zhangshang args[1] =123456 User security Check add method
我们看到获取了用户名和密码。这种方法在不修改配置文件的条件下即可获得add方法参数。
需要注意的是因为spring aop默认采用的是J2SE实现的动态代理机制,而动态代理机制只能对接口代理,所以上面的代码UserManager必须是接口。如果不使用接口则要使用cglib代理,因为cglib代理可以对类实现代理
如何使用cglib代理?
我们可以强制让spring aop使用cglib代理而不是jdk的动态代理。
只要在配置文件中将
<aop:config proxy-target-class="true">
并且增加cglib库到我们的用户自定义库中。
下面来测试:
我们首先删除UserManager接口,也删除MySecurityManager接口(这个接口本来就可以不要)。
修改Client的测试代码。剩下的代码如下所示。
注意我们先不使用cglib测试:
package com.lwf.aop; public class UserManagerImpl { public void add(String name, String password) { System.out.println("add method"); } public void del(String id) { System.out.println("del method"); } public void modify(int id, String name, String password) { System.out.println("modify method"); } }
package com.lwf.aop; 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+"] =" + args[i]); } } System.out.println("User security Check"); } }
package com.lwf.aop; import junit.framework.TestCase; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client extends TestCase{ public void testAop(){ ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); //UserManager userManager = (UserManager)ac.getBean("userManager"); //userManager.add("zhangshang", "123456"); //userManager.del("23"); UserManagerImpl userManager = (UserManagerImpl)ac.getBean("userManager"); userManager.add("zhangshang", "123456"); } }
<?xml version="1.0" encoding="UTF-8"?> <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.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" > <bean id="userManager" class="com.lwf.aop.UserManagerImpl"></bean> <bean id="mySecurityManager" class="com.lwf.aop.MySecurityManagerImpl"></bean> <aop:config > <aop:aspect id="mySecurity" ref="mySecurityManager"> <aop:pointcut expression="execution(* add*(..)) || execution(* del*(..))" id="allAddMethod" /> <aop:before method="checkSecurity" pointcut-ref="allAddMethod"/> </aop:aspect> </aop:config> </beans>
测试输出结果:
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'userManager' defined in class path resource [applicationContext.xml]:
Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException:
Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.
我们可以看到删除UserManager接口,而默认情况下使用JDK动态代理又需要该接口,所以报错,提示在这种情况下应该要使用cglib代理。
下面我们使用cglib代理,一加入cglib库,二修改配置文件如下:
<?xml version="1.0" encoding="UTF-8"?> <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.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" > <bean id="userManager" class="com.lwf.aop.UserManagerImpl"></bean> <bean id="mySecurityManager" class="com.lwf.aop.MySecurityManagerImpl"></bean> <aop:config proxy-target-class="true"> <aop:aspect id="mySecurity" ref="mySecurityManager"> <aop:pointcut expression="execution(* add*(..)) || execution(* del*(..))" id="allAddMethod" /> <aop:before method="checkSecurity" pointcut-ref="allAddMethod"/> </aop:aspect> </aop:config> </beans>
输出结果:
args[0] =zhangshang args[1] =123456 User security Check add method
通常我们使用JDK的动态代理就可以了,一般我们是对一些之前开发的系统,因为没有接口类,所以考虑使用cglib代理进行增加spring aop 的功能。
J2SE动态代理和CGLIB字节码生成代理的区别? * J2SE动态代理只针对接口进行代理,不能针对类 * CGLIB是针对类实现代理,主要对指定的类生成一个子类,并覆盖其中的方法,从而实现方法的拦截, 因为是通过继承,所以无法为final方法提供代理
- spring_aopXml.rar (8.8 KB)
- 下载次数: 20
发表评论
-
spring 3 和mybatis 3集成,并用junit4进行测试
2011-11-04 14:01 1479转:spring 3 和mybatis 3集成,并用junit ... -
java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource的解决方法
2011-11-03 16:17 2790用Myeclipse开发struts时,配置struts-co ... -
Strut2+Spring整合框架搭建
2011-11-02 22:19 1095参考:http://lukuijun.iteye.com/bl ... -
Spring+hibernate延迟加载报错解决办法之二
2010-06-29 17:28 1229在做删除操作的时候出现了org.springframework ... -
Spring+hibernate延迟加载报错解决办法之一
2010-06-29 17:25 1229我们在项目中一般都会使用Spring来管理Hibernate的 ... -
Spring项目中怎么配置log4j
2010-05-27 11:10 1602在spring项目中配置log4j http://blogg ... -
Spring与Struts集成方式三
2010-05-26 17:11 1165在集成方式一和二中我们是在web.xml中加入配置代码: & ... -
Spring与Struts集成方式二
2010-05-26 14:49 1063在集成方式一的基础上做改进: 第一种集成方案是在action ... -
Spring与Struts集成方式一
2010-05-25 14:13 931我们在Struts中在action类中调用Model层组件进行 ... -
Hibernate编程式事务与Spring Aop的声明式事务(spring与hibernate集成)
2010-05-24 17:15 2787采用编程式事务 事务主要分为:编程式事务和声明式事务 ... -
修改Eclipse配置,使得在配置文件中完成自动完成功能。
2010-05-24 15:10 2327在Eclipse中引入spring的配置文件applicati ... -
Spring AOP 概念理解及@AspectJ支持
2010-05-20 15:56 1598为了更好的理解Spring简介一文http://quicker ... -
Spirng AOP简介
2010-05-19 17:28 2304AOP 面向切面编程(AOP)通过提供另外一种思考程序结构的 ... -
Spring Bean中的自动装配——byType
2010-05-19 17:08 1194自动装配byType即通过查找类属性在配置文件中bean中定义 ... -
Spring Bean中的自动装配——byName
2010-05-19 16:34 3168自动装配(autowire)协作者 Spring IoC容器 ... -
Spring Bean的作用域:用Spring来解决Struts中Action的单实例问题
2010-05-19 10:47 1583我们知道在Struts中Action是从map中拿出来的,是单 ... -
Spring Bean定义的继承
2010-05-19 10:36 1344现有Bean2,Bean3,Bean4,Bean5 可观察到 ... -
Spring Set注入:基本类型、List、Map、Set、Array、Date类型注入
2010-05-18 15:58 11571Spring依赖注入有两种:构造器注入与Set注入 其中以S ... -
spring 初探
2010-05-17 16:53 1280Spring核心设计思想为IOC ... -
简单DAO层示例
2010-05-14 17:30 2115在使用spring架构之前,我们怎么设计自己的DAO层的呢? ...
相关推荐
5. **代理模式**:Spring AOP通过代理模式实现切面功能,有JDK动态代理和CGLIB代理两种方式,前者针对实现了接口的类,后者则用于未实现接口的类。 通过这个简单的案例,你应该对Spring AOP有了初步的理解。随着...
11. **Interceptor适配器 - AdvisorAdapter**:适配不同的Advice类型,使其能被AOP代理使用。 12. **AopProxy**:AOP代理接口,负责创建能够拦截并执行Advice的对象。Spring提供了两种实现:JDK Dynamic Proxy和...
2. **CGLIB代理**:当目标对象没有实现接口时,Spring会使用CGLIB库动态生成目标类的子类作为代理。 在Spring中,AOP配置通常在XML配置文件中进行,如下所示: ```xml <beans xmlns="http://www.springframework....
如果没有接口,Spring会使用CGLIB代理。 - **CGLIB代理**:CGLIB是一个代码生成库,用于创建目标对象的子类并拦截方法调用。 3. **AOP术语**: - **JoinPoint**:程序执行过程中的特定点,如方法的执行。 - **...
Spring框架提供了强大的AOP支持,下面介绍如何在Spring中配置和使用AOP: 1. **添加依赖**:首先需要在项目中添加AOP相关的JAR包,例如`aspectjweaver.jar`和`cglib-nodep-2.1_3.jar`等。 2. **配置AOP命名空间**:...
AOP实现基于代理技术,Spring支持JDK动态代理和CGLIB代理,根据目标类是否为接口选择合适的代理方式。 23. **对JDBC的支持** Spring提供了JDBC抽象层,简化了数据库操作,包括JdbcTemplate、SimpleJdbcTemplate等...