zoukankan      html  css  js  c++  java
  • Spring入门之AOP篇

    听了几节IT黑马营的SPRING课程,照着例程写了一个SPRING 中AOP的例子;

     一、准备工作

    下载复制或配置JAR包。图方便,我将下载的SPRING包都加上去了。另外还需要aspectj的两个包,见下图

    二、主要代码

    我在SRC目录下面,建了com.spring.aop的包,然后在下面写的代码。

    新建三个类文件Book.java Mybook.java和TestAop.java,主要测试AOP 的前后加载和环绕加载(横向抽取)

    Book.java代码:

    package com.spring.aop;
    
    public class Book {
        public void add() {
            System.out.println("Add.............");
        }
    }

    Mybook.java代码:

     1 package com.spring.aop;
     2 
     3 import org.aspectj.lang.ProceedingJoinPoint;
     4 
     5 public class Mybook {
     6     public void aopAdd() {
     7         
     8         System.out.println("前置!!!");
     9     }
    10 
    11     public void afterAdd() {
    12         
    13         System.out.println("后置!");
    14     }
    15     
    16     public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable  {
    17         
    18         System.out.println("环绕前!");
    19         proceedingJoinPoint.proceed();
    20         
    21         System.out.println("环绕后!");
    22     }
    23     
    24     
    25 }

    TestAop.java

    package com.spring.aop;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class TestAop {
        @Test
        public void testSevice() {
            ApplicationContext context = new ClassPathXmlApplicationContext("beans3.xml");
            Book book = (Book) context.getBean("book");
            book.add();
        }
    
    }

    TestAop.java是测试运行的文件,直接在这个文件中,点鼠标右键,选Run as ------Junit Test

    三、spring配置文件beans.xml

     1 <?xml version="1.0" encoding="UTF-8"?>  
     2 <beans xmlns="http://www.springframework.org/schema/beans"  
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     4        xmlns:aop="http://www.springframework.org/schema/aop"  
     5       xsi:schemaLocation="                                               
     6             http://www.springframework.org/schema/beans    
     7             http://www.springframework.org/schema/beans/spring-beans.xsd    
     8             http://www.springframework.org/schema/aop  
     9             http://www.springframework.org/schema/aop/spring-aop.xsd " >
    10            <!-- 1、配置对象 -->           
    11       <bean id = "book"   class="com.spring.aop.Book"></bean>
    12       <bean id = "mybook" class="com.spring.aop.Mybook"></bean>
    13       <!-- 2、配置AOP操作 -->
    14       <aop:config>
    15       <!-- 2.1配置切入点 -->
    16           <aop:pointcut  expression="execution(* com.spring.aop.Book.*(..))" id ="pointcut1" /> 
    17           <!-- 2.2配置切面  把增强用到方法上面-->
    18         <aop:aspect ref="mybook">
    19         <!--配置增强类型,method:增加类里使用哪个方法作为前置  -->
    20             <aop:before method="aopAdd" pointcut-ref="pointcut1"/>
    21             <aop:after-returning method="afterAdd" pointcut-ref="pointcut1"/>
    22             <aop:around method="around" pointcut-ref="pointcut1"/>
    23         </aop:aspect>
    24           
    25       </aop:config>
    26 </beans>

    其中,配置文件中前面的约束要先配好,我以前是从别的配置文件中复制过来的,一直没有学会从下载的包装中找约束。必须要有xmlns:aop="......"内容,否则会报错。

    首先配置用到两个类的<bean> 
    其次,配置AOP的核心:代码在<aop:config>和〈/aop:config>之间。aspect:pointcut是指定切入点的方法,expression="execution(* com.spring.aop.Book.*(..))"中,用*表示Book类的所有方法都可以被切入。id 是切入点名称,这个在后面的“切面和增强”配置中要用到,指明增强要用在什么地方;

    
    

     四、运行结果

    说明先执行“前置”aop:before指定的方法,再执行环绕aop:around指定的前方法。第三步执行环绕的后方法后置方法,最后执行后置方法 ,aspect:after-returning指定的方法 ;

  • 相关阅读:
    centos7.7环境下编译安装tengine2.3.2版本
    centos6.9安装python3.6.9独立的virtualenv环境,并且能正确引入ssl
    django在centos生产环境的部署
    django入门8之xadmin引入富文本和excel插件
    jenkins服务器使用python脚本rabbitmqadmin和shell对目标服务器进行管理
    django入门7之django template和xadmin常用技巧
    mysql5.7同步复制报错1060故障处理
    Centos7.6使用yum安装PHP7.2
    django中安全sql注入等
    django入门6引入验证码插件 django-simple-captcha
  • 原文地址:https://www.cnblogs.com/lrzy/p/8384845.html
Copyright © 2011-2022 走看看