这两天在云运营管理平台的ssh框架下加入aop编程,遇到不少问题,整理一下。
1。首先在spring配置文件中applicationContext.xml引入aop需要用到的标签,如下:
<?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" 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.5.xsd" default-autowire="autodetect"> <aop:config> <aop:aspect id="TestAspect" ref="aspectBean"> <!-- 配置com.spring.service包下所有类或接口的所有方法 --> <aop:pointcut id="businessService" expression="execution(* com.dc.action.loginoutAction.logout(..))" /> <aop:before pointcut-ref="businessService" method="doBefore"/> <aop:after pointcut-ref="businessService" method="doAfter"/> <aop:around pointcut-ref="businessService" method="doAround"/> <aop:after-throwing pointcut-ref="businessService" method="doThrowing" throwing="ex"/> </aop:aspect> </aop:config> <!-- 以下是部分bean定义 --> <bean id="aspectBean" class="com.dc.action.util.TestAspect"/> <bean id="loginoutAction" class="com.dc.action.loginoutAction"> <property name="userService"> <ref bean="userService" /> </property> <property name="codeService"> <ref bean="codeService" /> </property> <property name="rightListService"> <ref bean="rightListService" /> </property> </bean>
我们看到在aspectBean就是俗称的切面类,loginoutAction是待测试的bean,如果成功,loginoutAction中的logout方法会在执行前、执行后、报错时分别调用aspectBean中的相应方法。loginoutAction配置中其余的property是loginoutAction通过setter方式注入的其他spring的bean。注意在文件中不能有<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">内容。在http://www.chmfile.com/file-Spring25-c-YXBjLmh0bWw:.html中说spring-beans-2.0.dtd可以帮助我们快捷的使用JavaBeans,但在aop编程中加入会依法冲突。
以上分内容可参考http://www.iteye.com/topic/487828
2.jar包。aop需要用到如下jar包:
aopalliance-1.0.jar asm-2.2.3.jar asm-commons-2.2.3.jar asm-util-2.2.3.jar aspectjrt.jar aspectjweaver.jar cglib-nodep-2.1_3.jar
这几个jar包会和hibernate3的asm.jar;asm-attrs.jar;cglib-2.1.3.jar发生冲突。原因是spring和hibernate使用的asm版本不同,解决方法把冲突的hibernate3的jar包删掉即可。如果你的代码必须要求这些jar包共存,那可以试试修改下jar包,或者把代码修改下,目前我还没发现此情况。
3.aop切action时注意action不能集成actionSupport,否则的话会报类似$Proxy41.方法名()找不到的错误。这会对action造成一定的影响,因为此类中实现了很多的实用借口,提供了很多默认方法,这些默认方法包括国际化信息的方法、默认的处理用户请求的方法等。目前还没发现如何让他们共存。