目录
需求与分析:
需求:对百度网盘分享连接输入密码时尾部多输入的空格进行处理
分析:
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文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Spring-aop-demo1</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>18</maven.compiler.source>
<maven.compiler.target>18</maven.compiler.target>
</properties>
<dependencies>
<!--springFramework依赖,,,,由于AOP包与Context有依赖关系,所以不需要在进行导入aop的包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.23</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.11</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!-- Spring操作数据库有关的都要导这个包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<!-- Spring 整合mybtis的包,这个包是和mybatis的包版本是相对应的-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<!-- junit包-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Spring 操作test的 包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<!-- 导入Aspect(切面)的包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.8</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>
AOP总结:
概念:AOP(Aspect Oriented Programming)面向切面编程,一种编程范式
作用:在不惊动原始代码的情况下,为代码添加功能
核心概念:
代理(Proxy):SpringAOP的核心本质是采用代理模式实现的
连接点(JoinPoint):在SpringAOP中,理解为任意方法的执行
切入点(Aspect):匹配连接点的式子,也是具有共性功能的方法描述
通知(Advice):若干个方法的共性功能,在切入点执行,最终体现为一个方法
切面(Aspect):描述通知与切入点的对应关系
目标对象(Target):被代理的原始对象成为目标对象