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");
}
}
可能出现的问题:
解决方案:
(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)、修改完成,运用客户端测试就可以。
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接口实现:
Aop模块(LoggerAop.java):
spring的配置文件(applicationContext.xml):
客户端测试:
可能出现的问题:
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下载地址如下:
(用spring的注解可以实现但是用aspectJ的注解就报错)
@Before("execution(*com.xyz.myapp.dao.*.*(..))")
这个注解成功了
========springAOP实现(静态文件配置方式)
在上面方式的基础上修改如下:
(1)、修改aop模块代码(LoggerAop.java):
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)
<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/beans
<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)、修改完成,运用客户端测试就可以。
相关文章推荐
- Spring---->采用静态配置文件方式实现AOP
- Spring---->采用静态配置文件方式实现AOP
- Spring---->采用静态配置文件方式实现AOP
- Spring AOP之配置文件实现方式
- Spring Aop实现——Annotation方式(注解式)and Schema-based式(xml配置)
- 采用静态配置文件方式实现AOP
- spring学习(七)—AOP通过配置文件方式实现
- Spring Aop实现——Annotation方式(注解式)and Schema-based式(xml配置)
- Spring整理12 -- 面对切面(AOP)2 -- 配置文件的方式实现AOP
- Spring整理12 -- 面对切面(AOP)2 -- 配置文件的方式实现AOP
- 【AOP系列】(四)—采用Spring的静态配置文件实现AOP
- spring-aop 配置文件方式实现
- 用Spring配置文件或者注解方式实现AOP
- 用Spring配置文件或者注解方式实现AOP
- Spring学习笔记(四)Spring配置文件方式实现AOP
- Spring的xml文件配置方式实现AOP
- 8 -- 深入使用Spring -- 4...6 AOP代理:基于注解的XML配置文件的管理方式
- Spring(十二)使用Spring的xml文件配置方式实现AOP
- spring Aop 配置文件方式+JoinPoint获取参数
- [原]spring学习笔记9.3-Spring对Aop的支持-配置文件的方式