• Spring AOP案例:测试业务层接口万次执行效率


    目录

    案例:

    1.MyAdvise通知类(做通知,切入点,切入点与通知进行绑定)

    2.SpringConfig  Spring配置类(识别bean,与Aspect)

    3.jdbcConfig   (加载properties文件内的数据)

    4.MyBatisConfig   (MyBatis配置信息)

    5.AccountDao(数据连接层,持久层)

    6.domain(实体类)

    7.AccountServiceImpl(Service业务层,组装Dao的操作)

    8.AccountService(业务层接口)

    9.jdbc.properties(druid的配置信息,被SpringConfig加载,被JdbcConfig所引用)

    10.AccountServiceTest(测试类)

    11.pom.xml(这个也比较重要,在总结里面会讲,自己看也能明白,我写注释了)

    12.效果:

    13 .总结:


    案例:测量业务层接口的执行效率

    需求:任意业务层接口执行,均可显示其执行效率(执行时长)

    分析:

    1.业务功能:业务层接口执行前后,分别记录时间,求差值得到执行效率

    2.通知类型选择前后均可以增强的类型-----环绕通知

    补充说明:当前测试的接口执行效率仅仅是个理论值,并不是一次完整的执行过程

    案例:


    1.MyAdvise通知类(做通知,切入点,切入点与通知进行绑定)

    1. package com.itheima.aop;
    2. import org.aspectj.lang.ProceedingJoinPoint;
    3. import org.aspectj.lang.Signature;
    4. import org.aspectj.lang.annotation.Around;
    5. import org.aspectj.lang.annotation.Aspect;
    6. import org.aspectj.lang.annotation.Pointcut;
    7. import org.springframework.stereotype.Component;
    8. @Component
    9. @Aspect
    10. public class ServiceAdvise {
    11. //定义切入点,切入点时Service里面的所有方法,
    12. @Pointcut("execution(* com.itheima.service.*Service.*(..))")
    13. private void servicePt(){}
    14. //把切入点servicePt()与通知runSpeed()进行绑定
    15. @Around("ServiceAdvise.servicePt()")
    16. public void runSpeed(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    17. long star = System.currentTimeMillis();
    18. Signature signature = proceedingJoinPoint.getSignature();//代表了一次执行的签名信息
    19. String name = signature.getName(); //方法名
    20. Class declaringType = signature.getDeclaringType(); //表示方法的接口名
    21. for (int i = 0; i < 10000; i++) {
    22. proceedingJoinPoint.proceed();
    23. }
    24. Object proceed = proceedingJoinPoint.proceed();//调取原始操作,即时AccountServiceImpl的返回值
    25. long end = System.currentTimeMillis();
    26. System.out.println("业务层接口"+declaringType+"的"+name+"方法,执行万次的测试为"+(end - star)+"ms");
    27. }
    28. }

    2.SpringConfig  Spring配置类(识别bean,与Aspect

    1. package com.itheima.config;
    2. import org.springframework.context.annotation.*;
    3. @Configuration //代表了这是个配置类,以此与applicationContext.xml基础配置文件相当
    4. @ComponentScan("com.itheima") //包扫描
    5. @PropertySource("jdbc.properties") //加载properties配置文件
    6. @Import({JdbcConfig.class,MybatisConfig.class}) //加载JdbcConfig,MybatisConfig配置类
    7. @EnableAspectJAutoProxy //启动AOP的注解,切面
    8. public class SpringConfig {
    9. }

    3.jdbcConfig   (加载properties文件内的数据)

    1. package com.itheima.config;
    2. import com.alibaba.druid.pool.DruidDataSource;
    3. import org.springframework.beans.factory.annotation.Value;
    4. import org.springframework.context.annotation.Bean;
    5. import javax.sql.DataSource;
    6. public class JdbcConfig {
    7. @Value("${jdbc.driver}")
    8. private String driver;
    9. @Value("${jdbc.url}")
    10. private String url;
    11. @Value("${jdbc.username}")
    12. private String username;
    13. @Value("${jdbc.password}")
    14. private String password;
    15. //1.定义一个方法获取响应的bean
    16. //2.添加@Bean,表示当前方法的返回值是一个bean
    17. @Bean
    18. public DataSource dataSource(){
    19. DruidDataSource ds = new DruidDataSource();
    20. ds.setDriverClassName(driver);
    21. ds.setUrl(url);
    22. ds.setUsername(username);
    23. ds.setPassword(password);
    24. return ds;
    25. }
    26. }

    4.MyBatisConfig   (MyBatis配置信息)

    1. package com.itheima.config;
    2. import org.mybatis.spring.SqlSessionFactoryBean;
    3. import org.mybatis.spring.mapper.MapperScannerConfigurer;
    4. import org.springframework.context.annotation.Bean;
    5. import javax.sql.DataSource;
    6. public class MybatisConfig {
    7. //定义bean,SqlSessionFactoryBean,用于产生SqlSessionFactory对象
    8. @Bean
    9. public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
    10. SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
    11. ssfb.setTypeAliasesPackage("com.itheima.domain");//问实体类的位置,,扫描类型别名的包
    12. ssfb.setDataSource(dataSource);//替代的是jdbcConfig中的DataSource,也就是详细配置信息
    13. return ssfb;
    14. }
    15. //定义bean,返回MapperScannerConfigurer对象
    16. @Bean
    17. public MapperScannerConfigurer mapperScannerConfigurer(){
    18. MapperScannerConfigurer msc = new MapperScannerConfigurer();
    19. msc.setBasePackage("com.itheima.dao");//问映射在什么位置,由于注解完成了执行语句,所以不需要有映射文件
    20. return msc;
    21. }
    22. }

    5.AccountDao(数据连接层,持久层)

    1. package com.itheima.dao;
    2. import com.itheima.domain.Account;
    3. import org.apache.ibatis.annotations.Delete;
    4. import org.apache.ibatis.annotations.Insert;
    5. import org.apache.ibatis.annotations.Select;
    6. import org.apache.ibatis.annotations.Update;
    7. import java.util.List;
    8. public interface AccountDao {
    9. @Insert("insert into account(username,password)values(#{username},#{password})")
    10. void save(Account account);
    11. @Delete("delete from account where id = #{id} ")
    12. void delete(Integer id);
    13. @Update("update account set username = #{username} , password = #{password} where id = #{id} ")
    14. void update(Account account);
    15. @Select("select * from account")
    16. List findAll();
    17. @Select("select * from account where id = #{id} ")
    18. Account findById(Integer id);
    19. }

    6.domain(实体类)

    1. package com.itheima.domain;
    2. import java.io.Serializable;
    3. public class Account implements Serializable {
    4. private Integer id;
    5. private String name;
    6. private Double money;
    7. public Integer getId() {
    8. return id;
    9. }
    10. public void setId(Integer id) {
    11. this.id = id;
    12. }
    13. public String getName() {
    14. return name;
    15. }
    16. public void setName(String name) {
    17. this.name = name;
    18. }
    19. public Double getMoney() {
    20. return money;
    21. }
    22. public void setMoney(Double money) {
    23. this.money = money;
    24. }
    25. @Override
    26. public String toString() {
    27. return "Account{" +
    28. "id=" + id +
    29. ", name='" + name + '\'' +
    30. ", money=" + money +
    31. '}';
    32. }
    33. }

    7.AccountServiceImpl(Service业务层,组装Dao的操作)

    1. package com.itheima.service.impl;
    2. import com.itheima.dao.AccountDao;
    3. import com.itheima.domain.Account;
    4. import com.itheima.service.AccountService;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.stereotype.Service;
    7. import java.util.List;
    8. @Service
    9. public class AccountServiceImpl implements AccountService {
    10. @Autowired
    11. private AccountDao accountDao;
    12. public void save(Account account) {
    13. accountDao.save(account);
    14. }
    15. public void update(Account account){
    16. accountDao.update(account);
    17. }
    18. public void delete(Integer id) {
    19. accountDao.delete(id);
    20. }
    21. public Account findById(Integer id) {
    22. return accountDao.findById(id);
    23. }
    24. public List findAll() {
    25. return accountDao.findAll();
    26. }
    27. }

    8.AccountService(业务层接口)

    1. package com.itheima.service;
    2. import com.itheima.domain.Account;
    3. import java.util.List;
    4. public interface AccountService {
    5. void save(Account account);
    6. void delete(Integer id);
    7. void update(Account account);
    8. List findAll();
    9. Account findById(Integer id);
    10. }

    9.jdbc.properties(druid的配置信息,被SpringConfig加载,被JdbcConfig所引用)

    1. jdbc.driver=com.mysql.jdbc.Driver
    2. jdbc.url= jdbc:mysql://localhost:3306/db1?useSSL=false
    3. jdbc.username=root
    4. jdbc.password=root

    10.AccountServiceTest(测试类)

    1. package com.itheima.service;
    2. import com.itheima.config.SpringConfig;
    3. import org.junit.Test;
    4. import org.junit.runner.RunWith;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.test.context.ContextConfiguration;
    7. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    8. @RunWith(SpringJUnit4ClassRunner.class) //设置专用的类运行器
    9. @ContextConfiguration(classes = SpringConfig.class) //指定Spring文件的配置类
    10. public class AccountServiceTest {
    11. @Autowired
    12. private AccountService accountService;
    13. @Test
    14. public void testFindById(){
    15. System.out.println(accountService.findById(2));
    16. }
    17. @Test
    18. public void testFindAll(){
    19. System.out.println(accountService.findAll());
    20. }
    21. }

    11.pom.xml(这个也比较重要,在总结里面会讲,自己看也能明白,我写注释了)

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <modelVersion>4.0.0modelVersion>
    6. <groupId>org.examplegroupId>
    7. <artifactId>Spring-demo1-lifeCycleartifactId>
    8. <version>1.0-SNAPSHOTversion>
    9. <properties>
    10. <maven.compiler.source>18maven.compiler.source>
    11. <maven.compiler.target>18maven.compiler.target>
    12. properties>
    13. <dependencies>
    14. <dependency>
    15. <groupId>org.springframeworkgroupId>
    16. <artifactId>spring-contextartifactId>
    17. <version>5.3.23version>
    18. dependency>
    19. <dependency>
    20. <groupId>javax.annotationgroupId>
    21. <artifactId>javax.annotation-apiartifactId>
    22. <version>1.3.2version>
    23. dependency>
    24. <dependency>
    25. <groupId>com.alibabagroupId>
    26. <artifactId>druidartifactId>
    27. <version>1.2.11version>
    28. dependency>
    29. <dependency>
    30. <groupId>org.mybatisgroupId>
    31. <artifactId>mybatisartifactId>
    32. <version>3.5.6version>
    33. dependency>
    34. <dependency>
    35. <groupId>mysqlgroupId>
    36. <artifactId>mysql-connector-javaartifactId>
    37. <version>5.1.46version>
    38. dependency>
    39. <dependency>
    40. <groupId>org.springframeworkgroupId>
    41. <artifactId>spring-jdbcartifactId>
    42. <version>5.0.0.RELEASEversion>
    43. dependency>
    44. <dependency>
    45. <groupId>org.mybatisgroupId>
    46. <artifactId>mybatis-springartifactId>
    47. <version>1.3.0version>
    48. dependency>
    49. <dependency>
    50. <groupId>junitgroupId>
    51. <artifactId>junitartifactId>
    52. <version>4.12version>
    53. <scope>testscope>
    54. dependency>
    55. <dependency>
    56. <groupId>org.springframeworkgroupId>
    57. <artifactId>spring-testartifactId>
    58. <version>5.2.10.RELEASEversion>
    59. dependency>
    60. <dependency>
    61. <groupId>org.aspectjgroupId>
    62. <artifactId>aspectjweaverartifactId>
    63. <version>1.8.8version>
    64. dependency>
    65. dependencies>
    66. project>

    12.效果:

    13 .总结:

    我们学了两个模块,1是切入点的设置,2.是通知类型的学习,根据AOP的思想之下,感觉学的没什么难度,只是注解很难被记住,如果以后常常学习,这方面的模块的话,我感觉其实还是很简单的,毕竟,才刚刚学习Spring,后面的SpringCloud其实才是真正的难学习,抓紧时间赶进度吧,同志们。我们学习完Spring的两大核心内容之后,后面仅仅只有事务了,然后就是SpringMVC,然后就是SpringBoot,SpringCloud,任重道远同志们,我看到SpringCloud是真的多,给你们贴个图片。加油加油努力努力。

     

     

     

  • 相关阅读:
    R语言ggplot2可视化:使用ggpubr包的ggpie函数可视化饼图(pie chart)、为饼图不同区域添加标签
    面试汇总:这是一份全面&详细的Android面试指南
    Centos7通过SSH使用密钥实现免密登录
    SpringBoot:速成总结+实战——员工管理系统
    Navicat运行sql文件导入数据不全或导入失败
    二叉搜索树、B-树、B+树
    对KMP算法的一点碎碎念——上篇
    技术分享 | Jenkins 节点该如何管理?
    R语言爬虫程序自动爬取图片并下载
    docker安装gitlab 并dump出表结构
  • 原文地址:https://blog.csdn.net/qq_51272114/article/details/127393520