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();
        }
    }
  • 相关阅读:
    linux--->curl
    linux--->定时任务
    php--->无限级分类
    php--->自己封装的简易版mvc框架
    php--->单例模式封装mysql操作类
    Linux使用退格键时出现^H ^?解决方法
    错误日志
    linux下redis服务器安装使用 安装php的redis扩展 安装laravel下的redis
    php system()
    laravel redis的使用
  • 原文地址:https://www.cnblogs.com/xxdebug/p/8686588.html
Copyright © 2011-2022 走看看