import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
/**
-
Created with IntelliJ IDEA.
-
Description: If you don’t work hard, you will be a loser.
-
User: Listen-Y.
-
Date: 2020-10-23
-
Time: 21:46
*/
public class LogAfter implements AfterReturningAdvice {
@Override
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println(“方法名是:” + method.getName() + " 返回值是:" +
o);
}
}
- 注册上述所有类的bean
- 在xml文件中配置AOP, 配置切入点, 配置切入点与通知的连接
<beans xmlns=“http://www.springframework.org/schema/beans”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xmlns:context=“http://www.springframework.org/schema/context”
xmlns:aop=“http://www.springframework.org/schema/aop”
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
aop:config
<aop:pointcut id=“pointcut” expression=“execution(* aop.Service.*(…))”/>
<aop:advisor advice-ref=“logBefore” pointcut-ref=“pointcut”/>
<aop:advisor advice-ref=“logAfter” pointcut-ref=“pointcut”/>
</aop:config>
- 测试
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(“ApplicationContext.xml”);
Database database = context.getBean(“service”, Database.class);
database.add();
}
}
总结就是: 导包, 写实体类, 配置xml
-
导包和上面的依赖是一样的
-
实现切面实体
public class Log {
public void before() {
System.out.println(“=method begin=”);
}
public void after() {
System.out.println(“=method end=”);
}
}
- 配置xml文件
-
主要是配置切面、切入点和配置连接通知
-
要有需要被代理的类和切面的类的bean对象
-
在xml文件中配置aop, 与上面的不同的是, 这里需要配置切面, 然后配置切入点, 最后配置通知与切入点的连接
<beans xmlns=“http://www.springframework.org/schema/beans”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xmlns:context=“http://www.springframework.org/schema/context”
xmlns:aop=“http://www.springframework.org/schema/aop”
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
aop:config
<aop:aspect ref=“log”>
<aop:pointcut id=“pointcut” expression=“execution(* myProxy.Dao.*(…))”/>
<aop:before method=“before” pointcut-ref=“pointcut”/>
<aop:after method=“after” pointcut-ref=“pointcut”/>
</aop:aspect>
</aop:config>
- 测试
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(“ApplicationContext.xml”);
Database database = context.getBean(“dao”, Database.class);
database.add();
}
}
总结就是: 导包, 编写实体类, 但是是直接写切面, 然后在xml文件中配置切面和配置通知和切入点的连接, 使用这个方法最重要的就是配置xml
-
导入aspect的包, 还和上面的一样
-
给切面增注解@Aspect
-
给切面中的通知添加注解, 实现什么时候执行, 注解的后面还要配置切入点, 在环绕增强中around, 我们可以给定一个参数,去代表我们要执行的切入点
package annotationProxy;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
/**
-
Created with IntelliJ IDEA.
-
Description: If you don’t work hard, you will be a loser.
-
User: Listen-Y.
-
Date: 2020-10-24
-
Time: 13:58
*/
//添加注解表示这是一个切面
@Aspect
public class Log {
//添加注解, 配置切入点
@Before(“execution(* myProxy.Dao.*(…))”)
public void before() throws Throwable {
System.out.println(“=方法执行前=”);
}
@After(“execution(* myProxy.Dao.*(…))”)
public void after() {
System.out.println(“=方法执行后=”);
}
//环绕,使用这个注解可以给定一个参数, 去代表我们的切入点
@Around(“execution(* myProxy.Dao.*(…))”)
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println(“环绕前”);
//这个代表去执行这个方法
Object proceed = joinPoint.proceed();
System.out.println(“环绕后”);
}
}
-
配置xml文件, 开启aop的注解扫描
-
将被代理类和切面添加到容器中
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
感受:
其实我投简历的时候,都不太敢投递阿里。因为在阿里一面前已经过了字节的三次面试,投阿里的简历一直没被捞,所以以为简历就挂了。
特别感谢一面的面试官捞了我,给了我机会,同时也认可我的努力和态度。对比我的面经和其他大佬的面经,自己真的是运气好。别人8成实力,我可能8成运气。所以对我而言,我要继续加倍努力,弥补自己技术上的不足,以及与科班大佬们基础上的差距。希望自己能继续保持学习的热情,继续努力走下去。
也祝愿各位同学,都能找到自己心动的offer。
分享我在这次面试前所做的准备(刷题复习资料以及一些大佬们的学习笔记和学习路线),都已经整理成了电子文档
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门即可获取!
m: 33%;" />
感受:
其实我投简历的时候,都不太敢投递阿里。因为在阿里一面前已经过了字节的三次面试,投阿里的简历一直没被捞,所以以为简历就挂了。
特别感谢一面的面试官捞了我,给了我机会,同时也认可我的努力和态度。对比我的面经和其他大佬的面经,自己真的是运气好。别人8成实力,我可能8成运气。所以对我而言,我要继续加倍努力,弥补自己技术上的不足,以及与科班大佬们基础上的差距。希望自己能继续保持学习的热情,继续努力走下去。
也祝愿各位同学,都能找到自己心动的offer。
分享我在这次面试前所做的准备(刷题复习资料以及一些大佬们的学习笔记和学习路线),都已经整理成了电子文档
[外链图片转存中…(img-h13G4XNO-1712111936896)]
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门即可获取!