【JavaSpring】Aop案例

测定接口执行效率

描述:在接口执行前输出当前系统时间

开发模式:XML or 注解

思路分析:

1.导入坐标(pom.xml)

复制代码
<dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.25.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

2.制作连接点的方法(原始操作)

复制代码
package org.example.dao;

public interface BookDao {
    void save();
    void update();
}

package org.example.dao.impl;

import org.example.dao.BookDao;
import org.springframework.stereotype.Repository;

@Repository
public class BookDaoImpl implements BookDao {

    public void save() {
        System.out.println("book dao save");
        System.out.println(System.currentTimeMillis());
    }


    public void update() {
        System.out.println("book dao update");
    }

    public void delete() {
        System.out.println("book dao delete");
    }

    public void select() {
        System.out.println("book dao select");
    }
}

4.定义切入点

复制代码
package org.example.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect//当作Aop处理
public class MyAdvice {
    @Pointcut("execution(void org.example.dao.BookDao.update())")
    private void pt(){}
    @Before("pt()")
    public void method(){
        System.out.println(System.currentTimeMillis());
    }
}

5.注解驱动支持

复制代码
package org.example.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@ComponentScan("org.example")
@EnableAspectJAutoProxy//有用注解开发的AOP
public class SpringConfig {
}

测试

复制代码
package org.example;

import org.example.config.SpringConfig;
import org.example.dao.BookDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext ctx=new AnnotationConfigApplicationContext(SpringConfig.class);
        BookDao bookDao=ctx.getBean(BookDao.class);
        bookDao.save();
        bookDao.update();
    }
}
相关推荐
qq_124987075314 分钟前
基于SpringBoot学生学习历史的选课推荐系统的设计与实现(源码+论文+部署+安装)
java·spring boot·后端·学习·毕业设计·毕设
qq_4798754322 分钟前
C++ 鸭子类型” (Duck Typing)
开发语言·c++
廋到被风吹走36 分钟前
【Spring】事务管理深度解析|从原理到实战
java·spring
勇气要爆发1 小时前
【第一阶段—基础准备】第五章:Python模块和包管理(基础篇)—变形金刚的装备库
开发语言·python
lkbhua莱克瓦241 小时前
Java进阶——IO流
java·开发语言·笔记·学习方法·io流
韩立学长1 小时前
【开题答辩实录分享】以《自选便利店商品分类管理系统》为例进行选题答辩实录分享
java·mysql·web
阿杰同学1 小时前
Java中55种锁,高级面试题,最新面试题
java·开发语言
清晓粼溪1 小时前
SpringCloud01-基础概念
java·开发语言·spring cloud
路边草随风1 小时前
java实现发布flink yarn application模式作业
java·大数据·flink·yarn
华仔啊1 小时前
RabbitMQ 如何保证消息不丢失和不重复消费?掌握这 4 个关键点就够了
java·后端·rabbitmq