• SSM学习37:Aop入门案例(重点)


    2.1 需求分析

    案例设定:测算接口执行效率,但是这个案例稍微复杂了点,我们对其进行简化。

    简化设定:在方法执行前输出当前系统时间。

    对于SpringAOP的开发有两种方式,XML 和 ==注解==,我们使用哪个呢?

    因为现在注解使用的比较多,所以本次课程就采用注解完成AOP的开发。

    总结需求为:使用SpringAOP的注解方式完成在方法执行的前打印出当前系统时间。

    2.2 思路分析

    需求明确后,具体该如何实现,都有哪些步骤,我们先来分析下:

    1.导入坐标(pom.xml)

    2.制作连接点(原始操作,Dao接口与实现类)

    3.制作共性功能(通知类与通知)

    4.定义切入点

    5.绑定切入点与通知关系(切面)

    创建一个Maven项目

     

    导入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>SpringAopartifactId>
    8. <version>1.0-SNAPSHOTversion>
    9. <dependencies>
    10. <dependency>
    11. <groupId>org.springframeworkgroupId>
    12. <artifactId>spring-contextartifactId>
    13. <version>5.2.10.RELEASEversion>
    14. dependency>
    15. <dependency>
    16. <groupId>org.aspectjgroupId>
    17. <artifactId>aspectjweaverartifactId>
    18. <version>1.9.4version>
    19. dependency>
    20. <dependency>
    21. <groupId>org.springframeworkgroupId>
    22. <artifactId>spring-testartifactId>
    23. <version>5.2.10.RELEASEversion>
    24. dependency>
    25. dependencies>
    26. <properties>
    27. <maven.compiler.source>8maven.compiler.source>
    28. <maven.compiler.target>8maven.compiler.target>
    29. properties>
    30. project>

    添加BookDao和BookDaoImpl类

    1. package com.itheima.dao;
    2. public interface book {
    3. public void save();
    4. public void update();
    5. }
    6. package com.itheima.dao.impl;
    7. import com.itheima.dao.book;
    8. import org.springframework.stereotype.Repository;
    9. @Repository
    10. public class bookimpl implements book {
    11. @Override
    12. public void save() {
    13. System.out.println("方法1");
    14. }
    15. @Override
    16. public void update() {
    17. System.out.println("方法2");
    18. }
    19. }

    创建Spring的配置类

    1. package com.itheima.SpringConfig;
    2. import org.springframework.context.annotation.ComponentScan;
    3. import org.springframework.context.annotation.EnableAspectJAutoProxy;
    4. import org.springframework.stereotype.Component;
    5. @Component
    6. @ComponentScan("com.itheima")
    7. @EnableAspectJAutoProxy
    8. public class SpringConfig {
    9. }

     

    定义通知类和通知MyAdvice  

     

    1. package com.itheima.aop;
    2. import org.aspectj.lang.annotation.Aspect;
    3. import org.aspectj.lang.annotation.Before;
    4. import org.aspectj.lang.annotation.Pointcut;
    5. import org.springframework.stereotype.Component;
    6. @Component
    7. @Aspect
    8. public class bookaop {
    9. //绑定连接点
    10. @Pointcut("execution(void com.itheima.dao.book.save())")
    11. private void bb(){};
    12. //绑定切面
    13. @Before("bb()")
    14. //创建通知
    15. public void kk(){
    16. System.out.println("郭浩康加油");
    17. }
    18. }

    编写App运行类

    1. package com.itheima;
    2. import com.itheima.SpringConfig.SpringConfig;
    3. import com.itheima.dao.book;
    4. import org.springframework.context.ApplicationContext;
    5. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    6. public class APP {
    7. public static void main(String[] args) {
    8. ApplicationContext ctx=new AnnotationConfigApplicationContext(SpringConfig.class);
    9. book book = ctx.getBean(book.class);
    10. book.save();
    11. }
    12. }
    1. package com.itheima;
    2. import com.itheima.SpringConfig.SpringConfig;
    3. import com.itheima.dao.book;
    4. import org.junit.Test;
    5. import org.junit.runner.RunWith;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.test.context.ContextConfiguration;
    8. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    9. @RunWith(SpringJUnit4ClassRunner.class)
    10. @ContextConfiguration(classes = SpringConfig.class)
    11. public class test {
    12. @Autowired
    13. private book bk;
    14. @Test
    15. public void testsave(){
    16. bk.save();
    17. }
    18. }

    运行结果

     

  • 相关阅读:
    【Python大数据笔记_day09_hive函数和调优】
    Optional避免判空嵌套过多,优雅解决空指针异常
    PyTorch实现非极大值抑制(NMS)
    Spring Boot中Spring MVC的基本配置讲解与实战(包括静态资源配置,拦截器配置,文件上传配置及实战 附源码)
    基于DOTween插件实现金币飞行到指定位置功能
    项目进度网络图
    Kubernetes学习篇之数据加密
    深度解析Java虚拟机(JVM)的垃圾回收机制
    CSS @符规则(@font-face、@keyframes、@media、@scope等)
    KubeSphere 多行日志采集方案深度探索 
  • 原文地址:https://blog.csdn.net/weixin_51330376/article/details/127580957