[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.
相关文章
- 解决spring的java.lang.IllegalArgumentException异常
- SSH(Spring+Struts2+Hibernate)框架搭建步骤(含配置文件以及运行结果)
- ssh(Spring+Spring mvc+hibernate)——Emp.java
- jbpm与spring hibernate struts整合
- spring 启动属性注入
- [Spring boot] Read values from external properties file
- maven 创建spring boot 需要的配置[一]
- spring boot单元测试之十:用embedded-redis库做Redis的mock(spring boot 2.4.3)
- spring boot:thymeleaf给fragment传递参数的方法(spring boot 2.3.3)
- spring boot:thymeleaf模板中insert/include/replace三种引用fragment方式的区别(spring boot 2.3.3)
- spring boot:shardingsphere+druid整合seata分布式事务(spring boot 2.3.3)
- spring boot: 用thymeleaf嵌套循环展示多层数据(spring boot 2.3.2)
- spring boot单元测试之十:用embedded-redis库做Redis的mock(spring boot 2.4.3)
- spring boot:方法中使用try...catch导致@Transactional事务无效的解决(spring boot 2.3.4)
- spring boot:spring security+oauth2+sso+jwt实现单点登录(spring boot 2.3.3)
- spring boot:使用caffeine+redis做二级缓存(spring boot 2.3.1)
- 《Spring Boot Cookbook》阅读笔记
- Spring Boot:定制URL匹配规则
- spring boot报FileSizeLimitExceededException异常的解决方法
- Spring Boot为什么不需要额外安装Tomcat?
- Spring Boot 之spring.factories
- spring @Cacheable使用SpEL异常:org.springframework.expression.spel.SpelParseException: Unexpected token. Expected 'identifier' but was 'lcurly({)'
- spring security 动态 修改当前登录用户的 权限
- Spring Boot @EnableWebMvc 配置Long类型精度丢失, supportedMediaType 支持,addResourceHandlers配置;添加拦截器
- SSH框架入门学习之二(spring)
- spring 中的设计模式
- 009-Spring Boot 事件监听、监听器配置与方式、spring、Spring boot内置事件
- 【【SpringBoot系列】最详细demo--自定义日志脱敏组件,简单3 步完成 Spring Boot 的日志脱敏