Cool
Cool
Published on 2021-07-12 / 21 Visits
0
0

SpringAop 测试类

maven坐标

{alert type="info"} 一定要带这个包!! org.aspectj aspectjweaver 1.8.6 {/alert}

{alert type="warning"} 一定要带这个包!! 说两遍!!!! ::(喷) org.aspectj aspectjweaver 1.8.6 {/alert}

    <!--sring核心一带四-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.8.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version>
    </dependency>
    <!--测试一定要带-->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.6</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>4.3.8.RELEASE</version>
    </dependency>

applicationContext.xml spring 配置文件

<?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="customerService" class="com.liang.CustomerServiceImpl"> </bean>
    <bean id="myLog" class="com.liang.MyLog"> </bean>
    <!-- aop配置 -->
    <aop:config>
        <!-- 配置的代码写在此处 -->
        <aop:aspect id="logAdvice" ref="myLog">
           <!-- method: 用于指定通知类的增强方法名
             pointcut-ref: 用于指定引用的切入点表达值
                 expression 切入点表达式
                 id 切入点表达式的唯一标识符-->
            <aop:pointcut expression="execution(public void com.liang.CustomerServiceImpl.saveCustomer())"
                          id="p1"/>
            <aop:before method="printLog" pointcut-ref="p1"/>
        </aop:aspect>
    </aop:config>
</beans>

接口

package com.liang;
//业务层接口
public interface CustomerService {
    //保存客户
    public void saveCustomer();
    //修改客户
    public void updateCustomer(Integer id);
}

接口实现类

package com.liang;

import org.springframework.stereotype.Service;

//业务层实现类
public class CustomerServiceImpl implements CustomerService {
    @Override
    public void saveCustomer() {
        System.out.println("调用持久层,执行保存客户!");
    }
    @Override
    public void updateCustomer(Integer id) {
        System.out.println("调用持久层,执行修改客户:" + id);
    }
}

通知增强类((切面代码))

package com.liang;
//一个记录日志的类
public class MyLog {
	public void printLog() {
		System.out.println("MyLog类中的pringLog方法开始记录日志了!!!");
	}
}

单元测试类

package com.liang;

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.SpringRunner;

@ContextConfiguration(locations = "classpath:applicationContext.xml")
@RunWith(SpringRunner.class)
public class Aoptest {
    @Autowired
    private CustomerService customerService;
    @Test
    public void aVoid() {
        customerService.saveCustomer();
    }
}

效果

QQ截图20210712195037.png


Comment