package com.wanzi;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import java.util.Map;
@Aspect
@Component
public class RoutingAspect {
@Around("@annotation(routingWith)")
public Object routingWithDataSource(ProceedingJoinPoint proceedingJoinPoint, RoutingWith routingWith) throws Throwable{
String key = routingWith.value();
System.out.println(key);
return proceedingJoinPoint.proceed();
}
@AfterReturning(value = "execution(* *..afterReturn(java.util.Map))",returning = "output")
public void aopAfterReturning(JoinPoint joinPoint, Map<Integer,Integer> output){
output.put(1,3);
System.out.println("@AfterReturning修改了返回值" + output.size());
}
@Before(value = "execution(* *..before(String))")
public void aopBefore(JoinPoint joinPoint){
System.out.println(joinPoint.getArgs()[0]);
}
@After(value = "execution(* *..after(java.util.Map)) && args(output)",argNames = "output")
public void aopBefore(Map<Integer,Integer> output){
System.out.println("修改了传进来的参数,原始长度为"+output.size());
output.put(1,1);
}
}
//spring-aop
//
// 2:那怎么快速入手属于自己的spring-aop呢(或者说怎么在自己项目调用spring-aop这个框架呢)
// pre:环境[jdk8,maven3.5,intellj idea2018] 自己去新建一个maven项目
// a:首先要有spring-aop这个框架的代码,才能使用spring-aop,所以第一步就是在maven项目的依赖库添加自己所需要的依赖
//<dependencies>
//<dependency>
//<groupId>org.springframework.boot</groupId>
//<artifactId>spring-boot-starter-aop</artifactId>
//<version>3.2.2</version>
//</dependency>
//</dependencies>
// b:其次呢,spring-aop说到底就是依赖spring的。而spring现在有个框架叫做spring-boot,这是个狠角色,可以开箱即用很多框架。这个spring-aop自然也跟spring-boot对接了,所以我上面直接导入spring-boot-starter-aop这个依赖包,这个依赖包也是可以开箱即用,我们只要导入这个starter就能直接用,也就不需要再导入spring-boot了,当然这个包支持我们直接用spring-boot启动项目,那么接下来我们需要写个spring-boot的启动类了
// 我新建了个一个包,在包里面添加了这个启动类
//@SpringBootApplication
//public class MainRunApplication {
//
// public static void main(String args[]){
// new SpringApplication(MainRunApplication.class).run();
// }
//}
//c:spring-boot已经搭建完成,并且能启动了,那么我们接下来要熟悉spring-aop的用法了
// [-------------小葵花课堂--------------]
// | 1:spring-aop作用于方法
// | 2:spring-aop可以[前置通知before advice,后置通知after advice,环绕通知around advice,事后返回通知afterReturning advice,异常通知afterThrowingadvice]
// | 3:@Before,@After,@Around,@AfterReturning,@AfterThrowing这几个对应上诉的几个通知
//| 4:上述几个通知注解下的方法的参数一般有JoinPoint和ProceedingJoinPoint(环绕通知的参数)这两个
// [-------------小葵花课堂--------------]
// d:接下来试试第一个注解@Before
//我们先创建个类,并声明这是个Aspect类,并指明这是个控件Component
//@Aspect
//@Component
//public class RoutingAspect {
//}
//接下来,我们写个before的方法
//public void aopBefore(JoinPoint joinPoint){
// System.out.println(joinPoint.getArgs()[0]);
// }
// 然后再方法上面加入切入点
//@Before(value = "execution(* *..before(String))")
没有合适的资源?快使用搜索试试~ 我知道了~
2024-spring-aop学习项目
共16个文件
xml:6个
class:4个
java:4个
需积分: 0 0 下载量 123 浏览量
2024-01-26
09:49:40
上传
评论
收藏 18KB ZIP 举报
温馨提示
这个是上文描述的那个项目资料,spring -aop
资源推荐
资源详情
资源评论
收起资源包目录
study.zip (16个子文件)
study
pom.xml 812B
src
test
java
main
resources
java
com
wanzi
MainRunApplication.java 920B
RoutingAspect.java 4KB
RoutingWith.java 329B
MainTest.java 829B
.idea
.name 7B
uiDesigner.xml 9KB
workspace.xml 38KB
misc.xml 539B
compiler.xml 579B
encodings.xml 138B
target
classes
com
wanzi
RoutingAspect.class 3KB
RoutingWith.class 454B
MainRunApplication.class 2KB
MainTest.class 2KB
generated-sources
annotations
study.iml 81B
共 16 条
- 1
资源评论
明夕何夕帅
- 粉丝: 98
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功