【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();
    }
}
相关推荐
阿龍178710 小时前
媒体文件问题检测脚本(一)(python+ffmpeg)
开发语言·python
2501_9411118410 小时前
分布式日志系统实现
开发语言·c++·算法
培风图楠10 小时前
Java个人学习笔记
java·笔记·学习
梅梅绵绵冰10 小时前
SpringMVC的配置响应-页面跳转,回写数据
java
A***279510 小时前
后端服务限流配置,Spring Cloud Gateway
java·运维·数据库
222you10 小时前
SpringBoot对SpringMVC的整合
java·spring boot·后端
珹洺10 小时前
Java-Spring入门指南(三十二)Android SQLite数据库实战
java·数据库·spring
刘一说10 小时前
深入理解 Spring Boot 高级特性:条件化 Bean 注册机制
java·spring boot·后端
Han.miracle10 小时前
JavaEE ——多线程的线程安全集合类
java·java-ee
未来之窗软件服务10 小时前
幽冥大陆(二十三)python语言智慧农业电子秤读取——东方仙盟炼气期
开发语言·python·仙盟创梦ide·东方仙盟·东方仙盟sdk·东方仙盟浏览器