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指定的方法 ;

  • 相关阅读:
    Pytest 学习(二十七)- Jenkins+Allure+Pytest的持续集成
    Pytest 学习(二十六)- allure.dynamic 动态生成功能的使用
    Pytest 学习(二十五)- 解决pytest参数化测试标题都一样问题
    Pytest 学习(二十五)- allure 命令行参数【转】
    Pytest 学习(二十四)- @allure.severity 标记用例级别的使用
    Pytest学习(二十三)- allure 之 @allure.epic()、@allure.feature()、@allure.story() 的使用
    Spark
    asp.net core 5,0 项目中Add-Migration 执行报错 (可能不小心删掉了,安装后真的可以用了!!!)
    Asp.Net Core报错System.Text.Json.JsonException: A possible object cycle was detected which is not supp(配置导航属性的时候出现的错误)
    vs2019 netcore 5.0 连接mysql数据库踩下的坑。不要再测试其他 Pomelo.EntityFreameworkCore.Mysql,那些网上的都是针对3.x 根本就不行
  • 原文地址:https://www.cnblogs.com/lrzy/p/8384845.html
Copyright © 2011-2022 走看看