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();
        }
    }
  • 相关阅读:
    VB.NET与C# 语法区别展示
    利用 ASP.NET 的内置功能抵御 Web 攻击 (1)
    .NET 中获取调用方法名
    C# 6.0 的那些事
    .NET基础之自定义泛型
    汽车学习---汽车知识大全【all】
    Django学习---抽屉热搜榜分析【all】
    Python 系统学习梳理_【All】
    Python学习---装饰器/迭代器/生成器的学习【all】
    Java 系统学习梳理_【All】
  • 原文地址:https://www.cnblogs.com/xxdebug/p/8686588.html
Copyright © 2011-2022 走看看