spring学习之五(spring aop编程)
来源:互联网 发布:下载中国网络电视台直播 编辑:程序博客网 时间:2024/06/04 01:08
在了解这个之前个人感觉应该了解下反射,代理模式,动态代理等相关的内容比较好理解不然即使做出来效果感觉也不理解。动态代理本身感觉就有点难理解如果不太明白的可以参考下面的3个文章有便于理解相关内容:
《Javase 专题之 反射和动态代理》
《Javase 专题 反射和动态代理 aop》
AOP(面向方面编程)
AOP 是什么
AOP的全称是Aspect-Oriented Programming,面向方面编程,面向切面编程。在我们编写应用程序时,好多辅助功能如打印日志,安全验证,事务处理的代码分散在应用程序的各个角落,与业务逻辑代码高度耦合,不利于程序开发维护扩展。面向方面编程也可以说面向某一特定问题的编程。我们使用代理模式,把这些方面代码抽取出来,单独解决,提高了
程序的可维护性,可扩展性。使业务逻辑代码与这些方面代码解耦(分开),使业务逻辑组件安心做事。
先介绍spring aop 使用注解实现方式:
第一步spring的配置文件中(当然需要添加相应的jar文件支持)
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 打开Spring的Annotation支持 --> <context:annotation-config/> <!-- 设定Spring 去哪些包中找Annotation --> <context:component-scan base-package="com.wfg"/><!-- 打开基于Annotation的AOP --><aop:aspectj-autoproxy proxy-target-class="true"/> </beans>
第二步变形切面类
package com.wfg.aspect;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.springframework.stereotype.Component;/* * 把具有某一个特定的功能单独拿出来开发成一个模块,比如日志,安全检查等操作 * 使用面向切面的架构来完成 */@Component("logAspect") //交由spring管理@Aspect //注明该类为切面类public class LogAspect { /** * "execution(* com.wfg.dao.*.*(..))" * 第一个 * 表示返回值,即任意返回值 * 第二个* 表示com.wfg.dao下的任意类 * 第三个* 表示任意方法 * (..)表示任意参数 * @param jp */ @Before("execution(* com.wfg.dao.*.*(..))") public void logStrat(JoinPoint jp){ //获得执行对象 //System.out.println(jp.getTarget()); //获得执行方法 //System.out.println(jp.getSignature().getName()); //获得参数 //System.out.println(jp.getArgs()); Logger.info("增加日志logStrat"); } @After("execution(* com.wfg.dao.*.*(..))") public void logAfter(){ System.out.println("加入日志logAfter"); } @Around("execution(* com.wfg.dao.*.*(..))") public void logAround(ProceedingJoinPoint pjp) throws Throwable{ Logger.info("开始在Around中加入日志"); pjp.proceed();//执行程序 Logger.info("结束Around"); }}
在学习的过程中报下面的这个错误;我查相关文档发现aop注入式基于接口注入,直接注入类是不可以的
错误项目代码后面会上传也
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userAction': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao' defined in file [G:\SSHworkspace1\spring_apo_annotation\bin\com\wfg\dao\UserDao.class]: 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
spring sop xml实现方式
spring 配置文件和切面类的编写
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 打开Spring的Annotation支持 --> <context:annotation-config/> <!-- 设定Spring 去哪些包中找Annotation --> <context:component-scan base-package="com.wfg"/> <!-- 打开基于xml的AOP --> <aop:config > <!-- 定义切面 --> <aop:aspect id="mylogAspect" ref="logAspect"> <!-- 在哪些位置加入相应的Aspect --> <aop:pointcut expression="execution( * com.wfg.dao.*.*(..))" id="mypointcut"/> <aop:before method="logStrat" pointcut-ref="mypointcut"/> <aop:after method="logAfter" pointcut-ref="mypointcut"/> <aop:around method="logAround" pointcut-ref="mypointcut"/> </aop:aspect> </aop:config> </beans>
切面类
package com.wfg.aspect;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Before;import org.springframework.stereotype.Component;/* * 把具有某一个特定的功能单独拿出来开发成一个模块,比如日志,安全检查等操作 * 使用面向切面的架构来完成 */@Component("logAspect") //交由spring管理public class LogAspect { public void logStrat(JoinPoint jp){ //获得执行对象 //System.out.println(jp.getTarget()); //获得执行方法 //System.out.println(jp.getSignature().getName()); //获得参数 //System.out.println(jp.getArgs()); Logger.info("增加日志logStrat"); } public void logAfter(){ System.out.println("加入日志logAfter"); } public void logAround(ProceedingJoinPoint pjp) throws Throwable{ Logger.info("开始在Around中加入日志"); pjp.proceed();//执行程序 Logger.info("结束Around"); }}
项目代码
链接:http://pan.baidu.com/s/1geWBTWB 密码:donl
1 0
- spring学习之五(spring aop编程)
- Spring学习(五)之AOP
- Spring 基础教程之五:AOP编程介绍
- spring的AOP(五)----Spring AOP 编程(AspectJ )
- spring学习日志之五 AOP原理
- Spring(五)AOP
- spring(五)AOP注解学习
- Spring学习之AOP
- Spring学习之AOP
- 【Spring学习】之 AOP
- Spring学习之AOP
- Spring学习之AOP
- spring之AOP学习
- Spring学习之AOP
- Spring Framework 开发参考手册 之五 Spring AOP: Spring之面向方面编程
- 细谈Spring(五)spring之AOP底层大揭秘
- Spring(五)spring之AOP底层大揭秘
- Spring之面向方面编程(AOP)
- Linux系统如何支持NTFS文件系统?
- Unity3D说明文档翻译-Positioning GameObjects
- 最大公约数的两种求法
- Java之用厄拉多塞筛选法求n以内的所有素数
- 前端跨域的各种方法
- spring学习之五(spring aop编程)
- L1-009. N个数求和
- Java学习笔记(三)
- linux下一个网卡配置多个ip【虚拟ip】
- Git是怎样控制版本的
- 7个示例科普CPU Cache
- Linux常用命令
- 转载:性能调优攻略
- 洛谷 P3112 [USACO14DEC] 后卫马克Guard Mark