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());
    }
}

 

posted @ 2019-12-02 07:46  笪笠  阅读(211)  评论(0编辑  收藏  举报