Springbootフェースプログラミング

11927 ワード

Springbootフェースプログラミング

  • 1. インポート依存
  • 2. 通知class
  • 3. テスト出力
  • 1.依存のインポート

    
    	org.springframework.boot
    	spring-boot-starter-aop		
    
    

    2.classへの通知

    package com.example.demo;
    
    import lombok.extern.slf4j.Slf4j;
    import org.aspectj.lang.JoinPoint;
    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;
    import org.springframework.stereotype.Component;
    
    @Slf4j
    @Component
    @Aspect
    public class MyAspect {
    
    	@Before(value = "within(com.example.demo.service.*Impl)")
    	public void before(JoinPoint point) {
    		log.info("======= ======");
    		log.info("  >>> " + point.getSignature().getName());
    		log.info("  >>> " + point.getTarget());
    	}
    
    	@After(value = "within(com.example.demo.service.*Impl)")
    	public void after(JoinPoint point) {
    		log.info("======= ======");
    		log.info("  >>> " + point.getSignature().getName());
    		log.info("  >>> " + point.getTarget());
    	}
    
    	@Around(value = "within(com.example.demo.service.*Impl)")
    	public Object around(ProceedingJoinPoint point) throws Throwable {
    		log.info("======= ======");
    		log.info("  >>> " + point.getSignature().getName());
    		log.info("  >>> " + point.getTarget());
    		Object object = point.proceed();
    
    		log.info("======= ======");
    		return object;
    	}
    }
    

    3.テスト出力

    ======= ======                                  
      >>> test                                          
      >>> com.example.demo.service.AdviceTestImpl@76b0ae1b 
    ======= ======                                         
      >>> test                                          
      >>> com.example.demo.service.AdviceTestImpl@76b0ae1b 
                                                           
    ======= ======                                  
    ======= ======                                         
      >>> test                                          
      >>> com.example.demo.service.AdviceTestImpl@76b0ae1b