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

spring学习之五(spring aop编程)

2017-03-16 07:43 387 查看
在了解这个之前个人感觉应该了解下反射,代理模式,动态代理等相关的内容比较好理解不然即使做出来效果感觉也不理解。动态代理本身感觉就有点难理解如果不太明白的可以参考下面的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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring aop