您的位置:首页 > 编程语言 > Java开发

spring AOP实现(Annotation方式/静态文件配置方式)

2011-03-28 13:09 706 查看
总结:

spring对AOP的支持(1)

Aspect默认情况下不用实现接口,但对于目标对象(UserManagerImpl.java),在默认情况下必须实现接口,如果没有实现接口必须引入CGLIB库

我们可以通过Advice中添加一个JoinPoint参数,这个值会由spring自动传入,从JoinPoint中可以取得参数值、方法名等等

spring对AOP的支持(2)

1、如果目标对象实现了接口,默认情况下会采用JDK的动态代理实现AOP
2、如果目标对象实现了接口,可以强制使用CGLIB实现AOP
3、如果目标对象没有实现了接口,必须采用CGLIB库,spring会自动在JDK动态代理和CGLIB之间转换

如何强制使用CGLIB实现AOP?
*添加CGLIB库,SPRING_HOME/cglib/*.jar
*在spring配置文件中加入<aop:aspectj-autoproxyproxy-target-class="true"/>

JDK动态代理和CGLIB字节码生成的区别?
*JDK动态代理只能对实现了接口的类生成代理,而不能针对类
*CGLIB是针对类实现代理,主要是对指定的类生成一个子类,覆盖其中的方法
因为是继承,所以该类或方法最好不要声明成final

========springAOP实现(Annotation方式)

配置及步骤:

1、spring依赖库(用户库)
*SPRING_HOME/dist/spring.jar
*SPRING_HOME/lib/jakarta-commons/commons-logging.jar
*SPRING_HOME/lib/log4j/log4j-1.2.14.jar
*SPRING_HOME/lib/aspectj/*.jar
2、拷贝spring配置文件到src下

3、拷贝log4j配置文件到src下

4、采用Aspect定义切面

5、在Aspect定义Pointcut和Advice

6、启用AspectJ对Annotation的支持并且将Aspect类和目标对象配置到Ioc容器中

代码示例:

UserMananger接口实现:

packagecom.hmk.mananger; publicclassUserManangerImplimplementsUserMananger{ @Override publicvoiddelete(Stringusername){ System.out.println("----UserManangerImpl.delete----"); } @Override publicvoidsave(Stringusername,Stringpassword){ System.out.println("--UserManangerImpl.save--"); } }

Aop模块(LoggerAop.java):
packagecom.hmk.mananger; importorg.aspectj.lang.annotation.Aspect; importorg.aspectj.lang.annotation.Before; @Aspect publicclassLoggerAop{ /*这种配置方式可能造成编译不通过 @Pointcut("execution(*save*(..))") privatevoidsaveMethod(){}; */ @Before("execution(*save*(..))") publicvoidlogMananger(){ System.out.println("----LoggerAop.logMananger()------"); } }

spring的配置文件(applicationContext.xml):

<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <aop:aspectj-autoproxy/> <beanid="loggerAop"class="com.hmk.mananger.LoggerAop"/> <beanid="userMananger"class="com.hmk.mananger.UserManangerImpl"/> <!-- <aop:config> <aop:pointcutid="saveMethod"expression="execution(*save*(..))"/> <aop:aspectid="Logger"ref="loggerAop"> <aop:beforemethod="logMananger"pointcut-ref="saveMethod"/> </aop:aspect> </aop:config> --> </beans>

客户端测试:

packagecom.hmk.mananger; importorg.springframework.beans.factory.BeanFactory; importorg.springframework.context.support.ClassPathXmlApplicationContext; publicclassClient{ /** *@paramargs */ publicstaticvoidmain(String[]args){ BeanFactoryfactory=newClassPathXmlApplicationContext("applicationContext.xml"); UserManangeruserMananger=(UserMananger)factory.getBean("userMananger"); userMananger.save("zhangsan","123"); userMananger.delete("zhangsan"); } }

可能出现的问题:

Exceptioninthread"main"org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'userManager'definedinclasspathresource[applicationContext-bean.xml]:Initializationofbeanfailed;nestedexceptionisjava.lang.IllegalArgumentException:errorat::0can'tfindreferencedpointcutallAddMethod



解决方案:
(1)应该是aspectj与JDK1.6的兼容性不好,解决方法换成jdk1.5
或者升级aspectj,最新的是aspectj-1.6.6.jar下载地址如下:http://www.eclipse.org/downloads/download.php?file=/tools/aspectj/aspectj-1.6.6.jar(2)采用这种配置方式:
(用spring的注解可以实现但是用aspectJ的注解就报错)
@Before("execution(*com.xyz.myapp.dao.*.*(..))")
这个注解成功了

========springAOP实现(静态文件配置方式)

在上面方式的基础上修改如下:

(1)、修改aop模块代码(LoggerAop.java):

packagecom.hmk.mananger;

importorg.aspectj.lang.annotation.Aspect;
importorg.aspectj.lang.annotation.Before;

//@Aspect
publicclassLoggerAop{

/*
@Pointcut("execution(*save*(..))")
privatevoidsaveMethod(){};
*/

// @Before("execution(*save*(..))")
publicvoidlogMananger(){
System.out.println("----LoggerAop.logMananger()------");

}
}


(2)、修改spring配置文件(applicationContext.xml)

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd"><!--
<aop:aspectj-autoproxy/>
-->
<beanid="loggerAop"class="com.hmk.mananger.LoggerAop"/>
<beanid="userMananger"class="com.hmk.mananger.UserManangerImpl"/>

<aop:config>
<aop:pointcutid="saveMethod"
expression="execution(*save*(..))"/>
<aop:aspectid="Logger"ref="loggerAop">
<aop:beforemethod="logMananger"pointcut-ref="saveMethod"/>
</aop:aspect>
</aop:config>

</beans>


(3)、修改完成,运用客户端测试就可以。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: