[Spring Boot] Aspect Intro
Aspect-Oriented Programming (AOP) complements Object-Oriented Programming (OOP) by providing another way of thinking about program structure. The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect. Aspects enable the modularization of concerns such as transaction management that cut across multiple types and objects. (Such concerns are often termed crosscutting concerns in AOP literature.)
One of the key components of Spring is the AOP framework. While the Spring IoC container does not depend on AOP, meaning you do not need to use AOP if you don't want to, AOP complements Spring IoC to provide a very capable middleware solution. [1]
It is powerful tool, it allow you to automaticlly watching Classes execution, we can preform some opreation before it or after it. It is a little bit similar to Angular Router Guards, before we enter a router, we can use Guards to check Auth.
package com.in28minutes.spring.aop.springaop.aspect; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Configuration; //AOP //Configuration @Aspect @Configuration public class UseAccessAspect { private Logger logger = LoggerFactory.getLogger(this.getClass()); //What kind of method calls I would intercept //execution(* PACKAGE.*.*(..)) @Before("execution(* com.in28minutes.spring.aop.springaop.business.*.*(..))") public void before(JoinPoint joinPoint){ logger.info(" Check for user access "); logger.info(" Allowed execution for {}", joinPoint); } }
package com.in28minutes.spring.aop.springaop.business; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.in28minutes.spring.aop.springaop.data.Dao1; @Service public class Business1 { @Autowired private Dao1 dao1; public String calculateSomething(){ //Business Logic return dao1.retrieveSomething(); } }
@AfterReturning(value = "com.in28minutes.spring.aop.springaop.aspect.CommonJoinPointConfig.businessLayerExecution()", returning = "result") public void afterReturning(JoinPoint joinPoint, Object result) { logger.info("{} returned with value {}", joinPoint, result); } @After(value = "com.in28minutes.spring.aop.springaop.aspect.CommonJoinPointConfig.businessLayerExecution()") public void after(JoinPoint joinPoint) { logger.info("after execution of {}", joinPoint); }
Anytime 'Business1' is running, the Aspect will kick in.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2018-05-14 [Javascript Crocks] Apply a function in a Maybe context to Maybe inputs (curry & ap & liftA2)
2018-05-14 [Javascript Crocks] Recover from a Nothing with the `coalesce` Method
2018-05-14 [Javascript Crocks] Recover from a Nothing with the `alt` method
2017-05-14 [Recompose] Add Local State with Redux-like Reducers using Recompose
2017-05-14 [Recompose] Add Local State to a Functional Stateless Component using Recompose
2016-05-14 [Javascript] Advanced Console Log Arguments
2016-05-14 [Javascript] Log Levels and Semantic Methods