• [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.

  • 相关阅读:
    修改ssh默认端口
    网络配置
    nginx 反向代理
    nginx web 服务
    小白日记22:kali渗透测试之提权(二)--抓包嗅探
    小白日记21:kali渗透测试之提权(一)--本地提权
    小白日记20:kali渗透测试之后渗透测试阶段(一)--上传工具
    小白日记19:kali渗透测试之选择和修改EXP
    小白日记18:kali渗透测试之缓冲区溢出实例(二)--Linux,穿越火线1.9.0
    小白日记17:kali渗透测试之缓冲区溢出实例-windows,POP3,SLmail
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10865330.html
Copyright © 2020-2023  润新知