• Spring in action读书笔记(五) Spring aop简单示例


    1、基于javaConfig方式

    需要引入的jar包

    <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.1.9.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>5.1.9.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>5.1.9.RELEASE</version>
            </dependency>

    定义一个接口,作为切面中切点的目标对象

    public interface Run {
        void run(int number);
    }

    定义切面类TrackCounter,  totalCount属性用于记录run方法执行了多少次

    @Pointcut声明 Run接口中run方法为切点的目标对象,  @Before注解在执行目标方法前需要执行的方法上,

    package test;
    
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    
    @Aspect
    public class TrackCounter {
    
        //用来记录run方法执行了多少次
        private int totalCount;
    
        @Pointcut("execution(* test.Run.run(int)) && args(number)")
        public void tracked(int number) {
    
        }
    
        @Before("tracked(number)")
        public void countTrack(int number) {
            totalCount += 1;
        }
    
        public int getTotalCount() {
            return totalCount;
        }
    
    }

    配置TrackCounter类和Run实现类, 启用Aspect注解, 这里创建了一个Run的匿名实现类

    package test;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.EnableAspectJAutoProxy;
    
    @Configuration
    @EnableAspectJAutoProxy
    public class TrackCounterConfig {
    
        @Bean
        public Run run() {
            return count -> {
    
            };
        }
    
        @Bean
        public TrackCounter trackCounter() {
            return new TrackCounter();
        }
    }

    测试:

    package test;
    
    import org.junit.Assert;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = TrackCounterConfig.class)
    public class TrackCounterTest {
    
        @Autowired
        private Run run;
    
        @Autowired
        private TrackCounter counter;
    
        @Test
        public void test() {
            run.run(1);
            run.run(2);
    
            Assert.assertEquals(2, counter.getTotalCount());
        }
    }

     2、  基于xml方式

    接口类

    package test;
    
    public interface Count {
        void execute(int number);
    }

    接口实现类

    package test;
    
    public class CountImpl implements Count {
    
        public void execute(int number) {
    
        }
    
    }

    切面类TrackCounter

    package test;
    
    public class TrackCounter {
        //用来记录run方法执行了多少次
        private int totalCount;
    
    
        public void countTrack(int number) {
            totalCount += 1;
        }
    
        public int getTotalCount() {
            return totalCount;
        }
    }

    在resources目录新建spring-aop.xml文件,通过xml方式实现aop

    <?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"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
        <context:component-scan base-package="test"/>
    
        <bean class="test.CountImpl" id="count"/>
    
        <bean class="test.TrackCounter" id="trackCounter" />
    
        <aop:aspectj-autoproxy/>
    
        <aop:config>
            <aop:aspect ref="trackCounter">
                <aop:pointcut expression="execution(* test.Count.execute(int)) and args(number)" id="run"/>
    
                <aop:before method="countTrack" pointcut-ref="run" />
            </aop:aspect>
        </aop:config>
    </beans>

    测试类:

    package test;
    
    import org.junit.Assert;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:spring-aop.xml")
    public class TrackCounterTest {
    
        @Autowired
        private Count run;
    
        @Autowired
        private TrackCounter counter;
    
        @Test
        public void test() {
            run.execute(1);
            run.execute(2);
    
            Assert.assertEquals(3, counter.getTotalCount());
        }
    }
  • 相关阅读:
    Redis Cluster笔记
    http协议之 COOKIE
    lor框架代码分析
    PHP--SPL扩展学习笔记
    lua协程----ngx-lua线程学习笔记
    设计模式
    eclipse中使用maven创建springMVC项目
    一般处理程序里使用session对象
    .Dot NET Cored简介
    Spring 依赖注入(基本注入和自动适配注入)
  • 原文地址:https://www.cnblogs.com/wushengwuxi/p/11968643.html
Copyright © 2020-2023  润新知