需求:任意业务层接口执行均可显示其执行效率(执行时常)
分析:
业务功能:业务层接口执行前后分别记录时间,求插值得到执行效率
通知类型选择前后均可以增强的类型—>环绕通知
详情见下图:
最后在test文件中测试程序的时候不要忘记了获取bean的时候面向接口编程:
package com.Alvis.service;
import com.Alvis.config.SpringConfig;
import com.Alvis.domain.Student;
import com.Alvis.service.impl.StudentServiceImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class) // 设置类运行器
@ContextConfiguration(classes = SpringConfig.class)
public class StudentTest {
@Test
public void selectAll() {
ApplicationContext apx = new AnnotationConfigApplicationContext(SpringConfig.class);
StudentService studentService = apx.getBean(StudentService.class);
List<Student> selectAll = studentService.selectAll();
System.out.println(selectAll);
}
@Test
public void selectById() {
ApplicationContext apx = new AnnotationConfigApplicationContext(SpringConfig.class);
StudentService studentService = apx.getBean(StudentService.class);
Student selectById = studentService.selectById(2);
System.out.println(selectById);
}
}
========================================================
万次执行:com.Alvis.service.StudentService.selectAll--->2085ms
null
8月 26, 2022 11:06:03 上午 com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl info
信息: {dataSource-2} inited
万次执行:com.Alvis.service.StudentService.selectById--->1562ms
null
进程已结束,退出代码0