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

1、基于javaConfig方式

需要引入的jar包


            org.springframework
            spring-context
            5.1.9.RELEASE
        
        
            org.springframework
            spring-test
            5.1.9.RELEASE
        
        
            junit
            junit
            4.12
        
        
            org.springframework
            spring-aspects
            5.1.9.RELEASE
        

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

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



       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">

    package="test"/>

    class="test.CountImpl" id="count"/>

    class="test.TrackCounter" id="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("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());
    }
}

 

你可能感兴趣的:(Spring in action读书笔记(五) Spring aop简单示例)