spring学习之五(spring aop编程)
2017-03-16 07:43
387 查看
在了解这个之前个人感觉应该了解下反射,代理模式,动态代理等相关的内容比较好理解不然即使做出来效果感觉也不理解。动态代理本身感觉就有点难理解如果不太明白的可以参考下面的3个文章有便于理解相关内容:
《Javase 专题之 反射和动态代理》
《Javase 专题 反射和动态代理 aop》
AOP(面向方面编程)
AOP 是什么
AOP的全称是Aspect-Oriented Programming,面向方面编程,面向切面编程。在我们编写应用程序时,好多辅助功能如打印日志,安全验证,事务处理的代码分散在应用程序的各个角落,与业务逻辑代码高度耦合,不利于程序开发维护扩展。面向方面编程也可以说面向某一特定问题的编程。我们使用代理模式,把这些方面代码抽取出来,单独解决,提高了
程序的可维护性,可扩展性。使业务逻辑代码与这些方面代码解耦(分开),使业务逻辑组件安心做事。
在学习的过程中报下面的这个错误;我查相关文档发现aop注入式基于接口注入,直接注入类是不可以的
错误项目代码后面会上传也
切面类
项目代码
链接:http://pan.baidu.com/s/1geWBTWB 密码:donl
《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
相关文章推荐
- 重新学习之spring第二个程序,配置AOP面向切面编程
- Web框架梳理:第四章:Spring学习入门、Spring属性注入、AOP编程、注解开发
- SSH学习之——Spring面向方面编程AOP
- Spring3.0 学习-AOP面向切面编程_Spring AOP的XML配置模式
- spring学习--面向切面编程AOP和AspectJ
- spring学习笔记7--使用spring进行面向切面的(AOP)编程(1)注解方式实现
- Spring3.0 学习-AOP面向切面编程_Spring AOP的注解模式即Aspectj模式
- Spring Boot学习——AOP编程的简单实现
- 跟开振学习Spring AOP第一篇:开启约定编程之路
- Spring学习笔记(三)--面向切面编程AoP
- java+spring切面编程(aop)spring控制反转(ioc)+hibenrate对象关系映射(ORM) 学习网址
- [Spring]面向切面编程AOP【学习笔记】
- Spring学习笔记(四) JavaConfig创建Bean与AOP编程相结合的Demo
- spring aop编程(二)
- Spring Aop Step-By-Step 学习笔记(下)
- 用代码学习Spring:IoC、AOP
- Spring Aop Step-By-Step 学习笔记(上)
- Spring 2.0中 AOP的编程
- 用代码学习Spring:IoC、AOP
- 用代码学习Spring:IoC、AOP(上)