zoukankan      html  css  js  c++  java
  • 基于aspectj的aop的操作

    1.引入相关的jar包

    2.建两个类

    public class Book {
      public void add(){
          System.out.println("add-----------");
      }
    }
    import org.aspectj.lang.ProceedingJoinPoint;
    public class MyBook {
        public void before(){
            System.out.println("前置輸出----------");
        }
        public void after(){
            System.out.println("后置输出----------");
        }
        public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
            System.out.println("方法之前");
            proceedingJoinPoint.proceed();
            System.out.println("方法之后");
        }
    }

    3.在配置文件中完成相关的配置

    <?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 id="book" class="com.example.aop.Book"></bean>
    <bean id="myBook" class="com.example.aop.MyBook"></bean>
    <!-- 配置aop方式 -->
    <aop:config>
    <!-- 配置切入點 -->
      <aop:pointcut expression="execution(* com.example.aop.Book.*(..))" id="pointcut1"/>
    <!-- 配置切面
        把增强用到方法中去
     -->
       <aop:aspect ref="myBook">
       <!-- 配置增强类型
          method:增强类里面使用哪个方法作为前置
        -->
          <aop:before method="before" pointcut-ref="pointcut1"/>
          <aop:after method="after" pointcut-ref="pointcut1"/>
          <aop:around method="around" pointcut-ref="pointcut1"/>
       </aop:aspect>
    </aop:config>
    
    </beans>

    4.建一个测试类 ,测试输出

    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import com.example.aop.Book;
    import comexample.ano1.BookService;
    public class TestAno {
        @Test
        public void testService() {
            ApplicationContext context = new ClassPathXmlApplicationContext(
                    "bean3.xml");
            Book book=(Book) context.getBean("book");
            book.add();
        }
    }
  • 相关阅读:
    《将博客搬至CSDN》
    java-FileUtils(复制文件夹、复制文件、字符串直接写入文件中)(新手)
    java-FileUtils(读取、判断、获取)-(新手)
    一.MySQL入门基础
    二.压缩指令的应用
    一.档案与目录管理
    四.mysql演示银行转账
    三.实例演示insert/update/delect更新数据库
    二.数据库游标对象cursor与实例
    一.数据库连接对象connection
  • 原文地址:https://www.cnblogs.com/xxdebug/p/8686588.html
Copyright © 2011-2022 走看看