【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();
    }
}
相关推荐
LXS_3572 分钟前
C++常用容器(下)---stack、queue、list、set、map
开发语言·c++·学习方法·改行学it
愚者游世5 分钟前
list Initialization各版本异同
开发语言·c++·学习·程序人生·算法
Poetinthedusk15 分钟前
WPF应用跟随桌面切换
开发语言·wpf
shejizuopin19 分钟前
基于SSM的高校旧书交易系统的设计与实现(任务书)
java·mysql·毕业设计·论文·任务书·基于ssm的·高校旧书交易系统的设计与实现
Hello World . .20 分钟前
数据结构:二叉树(Binary tree)
c语言·开发语言·数据结构·vim
叫我辉哥e121 分钟前
新手进阶Python:办公看板升级交互式可视化+移动端适配+多终端同步
开发语言·python
1candobetter26 分钟前
JAVA后端开发——Spring Boot 组件化自动配置机制
java·开发语言·spring boot
码农小卡拉29 分钟前
MyBatis-Flex 全面解析与实战教程:轻量高效的 MyBatis 增强方案
java·mybatis
没有bug.的程序员29 分钟前
Spring Boot 与 Sleuth:分布式链路追踪的集成、原理与线上故障排查实战
java·spring boot·分布式·后端·分布式链路追踪·sleuth·线上故障排查
一个网络学徒30 分钟前
python练习3
开发语言·python