Spring之注解实现aop(面向切面编程)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bbj12345678/article/details/81625298

1:Aop(aspect object programming)面向切面编程,名词解释:
    1.1:功能:让关注点代码与业务逻辑代码分离
    1.2:关注点
        重复代码就叫做关注点
    1.3:切面
        关注点形成的类,就叫做切面(类)
        面向切面编程,就是指对很多功能都有的重复代码抽取,再在运行的时候往业务方法上动态植入"切面类代码";
    1.4:切入点
        执行目标对象方法,动态植入切面代码
        可以通过切入点表达式,指定拦截那些类的那些方法,给指定的类在运行的时候植入切面类代码;
2:注解方式实现aop编程
    2.1:开发步骤


 (1):先引入aop相关的jar文件
            spring-aop-3.2.5.RELEASE.jar【去spring3.2源码里面找】
            aopalliance.jar【去spring2.5源码/lib/aopalliance文件里面找】
            aspectjweaver.jar【去spring2.5源码/lib/aspectj文件里面找】或者【aspectj-1.8.2/lib/aspectjweaver.jar】
            aspectjrt.jar【去spring2.5源码/lib/aspectj文件里面找】或者【aspectj-1.8.2/lib/aspectjrt.jar】

        《注意:用到的spring2.5版本的jar本舰,如果用jd1.7版本可能会出现问题,
              需要升级以下aspectj组件,即使用aspectj-1.8.2版本中提供的jar文件aspectjweaver.jar和aspectjrt.jar》    


(2)bean.xml中引入aop名称空间
      技巧:找到文件spring-framework-3.2.5.RELEASE/docs/spring-framework-reference/htmlsingle
         打开index.html搜索xmlns:aop然后找到下面红色三句话,分别拷贝到bean.xml中
         <?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"

          xsi:schemaLocation="http://www.springframework.org/schema/beans

            http://www.springframework.org/schema/beans/spring-beans.xsd

          http://www.springframework.org/schema/aop

          http://www.springframework.org/schema/aop/spring-aop.xsd">

      拷贝之后的bean.xml如下所示:

  <?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:p="http://www.springframework.org/schema/p"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xsi:schemaLocation="
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/aop
          http://www.springframework.org/schema/aop/spring-aop.xsd">
 
 </beans> 

(3):bean.xml中开启aop注解扫描,如下配置所示:

  <?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:p="http://www.springframework.org/schema/p"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xsi:schemaLocation="
         http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
 
       <!-- 开启注解扫描 -->
       <context:component-scan base-package="com.bie.aop"></context:component-scan>   
         
       <!-- 开启aop注解方式,默认为false -->    
       <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
       
 </beans>        

(4):开始写一个切面类,源码如下所示:

  package com.bie.aop;
  
  import org.aspectj.lang.annotation.After;
  import org.aspectj.lang.annotation.Aspect;
  import org.aspectj.lang.annotation.Before;
  import org.aspectj.lang.annotation.Pointcut;
  import org.springframework.stereotype.Component;
  
  
 /** 
 * @author BieHongLi 
 * @version 创建时间:2017年3月28日 下午9:10:43 
 * @Aspect:指定当前类为切面类
 */
 @Component  //加入到IoC容器
 @Aspect  //指定当前类为切面类
 public class Aop {
 
     //指定切入点表达式,拦截那些方法,即为那些类生成代理对象
     //@Pointcut("execution(* com.bie.aop.UserDao.save(..))")  ..代表所有参数
    //@Pointcut("execution(* com.bie.aop.UserDao.*())")  指定所有的方法     //@Pointcut("execution(* com.bie.aop.UserDao.save())") 指定save方法
    
     @Pointcut("execution(* com.bie.aop.UserDao.*(..))")
    public void pointCut(){
        
     }
     
     @Before("pointCut()")
     public void begin(){
         System.out.println("开启事务");
     }
     
     @After("pointCut()")
     public void close(){
         System.out.println("关闭事务");
     }
     
 }

猜你喜欢

转载自blog.csdn.net/bbj12345678/article/details/81625298