目录
需求与分析:
需求:对百度网盘分享连接输入密码时尾部多输入的空格进行处理
分析:
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文件
- "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.0modelVersion>
-
- <groupId>org.examplegroupId>
- <artifactId>Spring-aop-demo1artifactId>
- <version>1.0-SNAPSHOTversion>
-
- <properties>
- <maven.compiler.source>18maven.compiler.source>
- <maven.compiler.target>18maven.compiler.target>
- properties>
-
-
- <dependencies>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-contextartifactId>
- <version>5.3.23version>
- dependency>
- <dependency>
- <groupId>javax.annotationgroupId>
- <artifactId>javax.annotation-apiartifactId>
- <version>1.3.2version>
- dependency>
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>druidartifactId>
- <version>1.2.11version>
- dependency>
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatisartifactId>
- <version>3.5.6version>
- dependency>
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <version>5.1.46version>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-jdbcartifactId>
- <version>5.0.0.RELEASEversion>
- dependency>
-
- <dependency>
- <groupId>org.mybatisgroupId>
- <artifactId>mybatis-springartifactId>
- <version>1.3.0version>
- dependency>
-
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.12version>
- <scope>testscope>
- dependency>
-
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-testartifactId>
- <version>5.2.10.RELEASEversion>
- dependency>
- <dependency>
- <groupId>org.aspectjgroupId>
- <artifactId>aspectjweaverartifactId>
- <version>1.8.8version>
- dependency>
- <dependency>
- <groupId>log4jgroupId>
- <artifactId>log4jartifactId>
- <version>1.2.17version>
- dependency>
-
- dependencies>
- project>
概念:AOP(Aspect Oriented Programming)面向切面编程,一种编程范式
作用:在不惊动原始代码的情况下,为代码添加功能
核心概念:
代理(Proxy):SpringAOP的核心本质是采用代理模式实现的
连接点(JoinPoint):在SpringAOP中,理解为任意方法的执行
切入点(Aspect):匹配连接点的式子,也是具有共性功能的方法描述
通知(Advice):若干个方法的共性功能,在切入点执行,最终体现为一个方法
切面(Aspect):描述通知与切入点的对应关系
目标对象(Target):被代理的原始对象成为目标对象