alpha
alpha

Reputation: 57

i cannot see the aspect logs when i run the method to be intercepted

thats my code and this is only showing the logs as method is executed in intellij INFO: Publishing comment: Demo comment. Also added the dependencies but still i cant see whats the error.

package com.alpha;

import com.alpha.exampleApp.Comment;
import com.alpha.servoces.CommentServoce;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public static void main(String[] args) {
        var context = new AnnotationConfigApplicationContext(ComponentConfig.class);

        var comment = new Comment();
        comment.setAuthor("Laurentiu");
        comment.setText("Demo comment");

        var commentServoce = context.getBean(CommentServoce.class);
        commentServoce.publishComment(comment);


    }
}

this is aspect class->

package com.alpha;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

import java.util.Arrays;
import java.util.logging.Logger;

@Aspect
public class LogAspect {
    private Logger logger = Logger.getLogger(LogAspect.class.getName());

    @Around("execution(* com.alpha.servoces.*.*(..))")
    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
            String methodName = joinPoint.getSignature().getName();
            Object[] arguments = joinPoint.getArgs();
            logger.info("Method " + methodName +
                    " with parameters " + Arrays.asList(arguments) +
                    " will execute");
            Object returnedByMethod = joinPoint.proceed();
            logger.info("Method executed and returned " + returnedByMethod);
            return returnedByMethod;

    }
}

this is my config class->

package com.alpha;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.alpha.servoces")
public class ComponentConfig {
    @Bean
    public LogAspect loggingAspect(){
        LogAspect logAspect = new LogAspect();
        return logAspect;
    }
}

this is the class in which i want the method intercepted->

package com.alpha.servoces;
import com.alpha.exampleApp.Comment;
import org.springframework.stereotype.Service;
import java.util.logging.Logger;

@Service
public class CommentServoce {
    private Logger logger = Logger.getLogger(CommentServoce.class.getName());

    public String publishComment(Comment comment) {
        logger.info("Publishing comment: " + comment.getText());
        return "SUCCESS";
    }
}

i wanted to see the logs by intercepting the method through aspects in spring but i couldnt see the logs

Upvotes: 0

Views: 22

Answers (1)

Julian
Julian

Reputation: 4085

Just annotate your ComponentConfig class with @EnableAspectJAutoProxy and your interception will work

Upvotes: 1

Related Questions