Home > front end >  @BeforeAdvise is not working for same method names in Spring BOOT
@BeforeAdvise is not working for same method names in Spring BOOT

Time:01-19

I am learning Spring AOP and tried to call the @BeforeAdvise for two Different methods in different classes. But the log added in the first call is getting printed only. Please check the code below and let me know what is not correct here.

MainClass

package com.example.aspects;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.example.aspects.AopClasses.DemoConfig;

@SpringBootApplication
public class AspectsApplication {
    public static void main(String[] args) {
        SpringApplication.run(AspectsApplication.class, args);
    }
}

AspectClass

package com.example.aspects.Acpects;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class MyLoggingDemo {

    private static final Logger logger = LogManager.getLogger(MyLoggingDemo.class);

    @Before("execution(public String test())")
    public void beforeCheck() {
        logger.info("Before Check !!!!!!!!!!!! " System.currentTimeMillis() );  
    }   
}

ConfigClass

package com.example.aspects.AopClasses;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@EnableAspectJAutoProxy
@ComponentScan("com.example.aspects")
public class DemoConfig {

}

TestClass- Where the first call is made to the function

package com.example.aspects.AopClasses;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/")
public class tesTClass {

    private static final Logger logger = LogManager.getLogger(tesTClass.class);
     MemberShipDAO dao = new MemberShipDAO();

    @RequestMapping(value = "/")
    public String test() {
        logger.info("Hey I am here");
        return "HEy There I am Working !!";
    }
    
    @RequestMapping(value ="/test")
    public String testing() {
        logger.info("MemberShipDAO Call");
    return  dao.test();     // method with same name called here again
    }   
}

MemberShipDAO - Declared the test method again here

package com.example.aspects.AopClasses;

import org.springframework.stereotype.Component;

@Component
public class MemberShipDAO {

    public String test() {
        return "HEy!!";
    }
}

Console

2022-01-18 11:37:52.794  INFO 8032 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-01-18 11:37:52.796  INFO 8032 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2022-01-18 11:37:52.802  INFO 8032 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 5 ms
2022-01-18 11:37:52.811  INFO 8032 --- [nio-8080-exec-1] c.example.aspects.Acpects.MyLoggingDemo  : Before Check !!!!!!!!!!!! 1642486072811
2022-01-18 11:37:52.854  INFO 8032 --- [nio-8080-exec-1] c.example.aspects.AopClasses.tesTClass   : Hey I am here
2022-01-18 11:37:57.383  INFO 8032 --- [nio-8080-exec-3] c.example.aspects.AopClasses.tesTClass   : MemberShipDAO Call

CodePudding user response:

Change this

MemberShipDAO dao = new MemberShipDAO();

to this

@Autowired MemberShipDAO dao;

Spring's magic for the AOP happens only when you use the instance created by Spring. Behind the scene, Spring instantiated your dao and wrapped it with a proxy that makes a call to your aspect.

  •  Tags:  
  • Related