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();
        }
    }
  • 相关阅读:
    Java线程面试题 Top 50 (转载)
    Java并发编程:volatile关键字解析
    转:【创龙TMS320C6748开发板试用】相关软件的安装与基本设置+CCS安装失败分析
    Linux格式化分区报错Could not start /dev/sda No such file or directory 解决办法
    转:用 git 下载 uboot 源码
    转:堆(heap)和栈(stack)有什么区别??
    转:数字信号处理的学习资源
    转:VC中WORD,DWORD,unsigned long,unsigned short的区别(转)
    转:ASCII码表_全_完整版
    转:CFile::Seek
  • 原文地址:https://www.cnblogs.com/xxdebug/p/8686588.html
Copyright © 2011-2022 走看看