Spring AOP案例:百度网盘密码数据兼容处理 与 SpringAOP总结

目录

需求与分析:

代码实现:

AOP总结:


需求与分析:

需求:对百度网盘分享连接输入密码时尾部多输入的空格进行处理

分析:

1.在业务方法执行前对所有的输入参数进行格式处理——trim();

2.使用处理后的参数调用原始方法——环绕通知(around)中存在对原始方法的调用

代码实现:

DataAdvise  AOP 通知类

package com.itheima.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;


@Component    //让1配置类知道是bean
@Aspect       //让配置类知道是造Aop,去识别一下的内容
public class DataAdvise {

    //定义切入点
    @Pointcut("execution(boolean com.itheima.service.ResourcesService.openURL(*,*))")
    private void servicePt(){}

    //连接切入点
    @Around("servicePt()")
    public Object trimStr(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Object[] args = proceedingJoinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            //判断参数是否时String
            if (args[i].getClass().equals(String.class)){
                args[i]=args[i].toString().trim();
            }else{
                System.out.println("不是字符串");
            }
        }
        Object proceed = proceedingJoinPoint.proceed(args);
        return proceed;
    }

}

SpringConfig  Spring配置类

package com.itheima.config;

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

@Configuration                   //说明此文件为Spring配置类
@ComponentScan("com.itheima")    //包扫描,加载bean
@EnableAspectJAutoProxy          //启动了MyAdvise内的Aspect注解,
public class SpringConfig {
}

ResourcesDao  数据接口

package com.itheima.dao;

public interface ResourcesDao {

    boolean readResources(String url,String password);
}

ResourcesDaoImpl 

package com.itheima.dao.impl;

import com.itheima.dao.ResourcesDao;
import org.springframework.stereotype.Repository;

@Repository
public class ResourcesDaoImpl implements ResourcesDao {


    @Override
    public boolean readResources(String url, String password) {
        System.out.println(password.length());

        //避免空指针
        return "root".equals(password);
    }
}

ResourcesService  数据操作

package com.itheima.service;

public interface ResourcesService {

    public boolean openURL(String url,String password);

}

ResourcesServiceImpl

package com.itheima.service.impl;

import com.itheima.dao.ResourcesDao;
import com.itheima.service.ResourcesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ResourcesServiceImpl implements ResourcesService {

    @Autowired
    private  ResourcesDao resourcesDao;

    @Override
    public boolean openURL(String url, String password) {
        return resourcesDao.readResources(url,password);
    }
}

App  main方法类

package com.itheima;

import com.itheima.config.SpringConfig;
import com.itheima.service.ResourcesService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;


public class App {
    public static void main(String[] args) {

        //获取Java配置类
        AnnotationConfigApplicationContext acct = new AnnotationConfigApplicationContext(SpringConfig.class);

        //获取bean
        ResourcesService bean = acct.getBean(ResourcesService.class);

        //获取方法
        boolean root = bean.openURL("http://www.baidu.com/haha", "root   ");

        System.out.println(root);

    }
}

pom.xml文件



    4.0.0

    org.example
    Spring-aop-demo1
    1.0-SNAPSHOT

    
        18
        18
    


    
        
        
            org.springframework
            spring-context
            5.3.23
        
        
            javax.annotation
            javax.annotation-api
            1.3.2
        
        
            com.alibaba
            druid
            1.2.11
        
        
            org.mybatis
            mybatis
            3.5.6
        
        
            mysql
            mysql-connector-java
            5.1.46
        
        
        
            org.springframework
            spring-jdbc
            5.0.0.RELEASE
        
        
        
            org.mybatis
            mybatis-spring
            1.3.0
        
        
        
            junit
            junit
            4.12
            test
        
        
        
            org.springframework
            spring-test
            5.2.10.RELEASE
        

        
            org.aspectj
            aspectjweaver
            1.8.8
        
        
            log4j
            log4j
            1.2.17
        

    

AOP总结:

概念:AOP(Aspect Oriented Programming)面向切面编程,一种编程范式

作用:在不惊动原始代码的情况下,为代码添加功能

核心概念:

    代理(Proxy):SpringAOP的核心本质是采用代理模式实现的

    连接点(JoinPoint):在SpringAOP中,理解为任意方法的执行

    切入点(Aspect):匹配连接点的式子,也是具有共性功能的方法描述

Spring AOP切入点表达式+语法格式+通配符+书写技巧_我的猴子的博客-CSDN博客icon-default.png?t=M85Bhttps://blog.csdn.net/qq_51272114/article/details/127382125?spm=1001.2014.3001.5502

    通知(Advice):若干个方法的共性功能,在切入点执行,最终体现为一个方法

Spring AOP通知的类型+通知的案例_我的猴子的博客-CSDN博客icon-default.png?t=M85Bhttps://blog.csdn.net/qq_51272114/article/details/127383556?spm=1001.2014.3001.5502

    切面(Aspect):描述通知与切入点的对应关系

    目标对象(Target):被代理的原始对象成为目标对象

你可能感兴趣的:(Spring,java,开发语言,spring,后端)