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();
        }
    }
  • 相关阅读:
    实现在Android本地视频播放器开发
    敏捷开发的4个中心思想
    PHP如何大幅度提升运行效率? 把它编译成机器码!
    卸载Oracle数据库(有图有真相)
    宁波理工邀请赛 c zoj3185解题报告
    FRG图像文件格式的压缩质量
    另类的文件夹加密(批处理实现)
    代码详查中的自尊心
    [C# 网络编程系列]专题十:实现简单的邮件收发器
    php中获得客户端,服务器ip
  • 原文地址:https://www.cnblogs.com/xxdebug/p/8686588.html
Copyright © 2011-2022 走看看