我无法在运行要拦截的方法时看到方面日志。

huangapple go评论157阅读模式
英文:

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

问题

这是我的代码,只显示在IntelliJ中执行方法时的日志记录
INFO: 发布评论:演示评论。还添加了依赖项,但我仍然看不到错误在哪。

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("演示评论");

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

这是切面类->

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("方法 " + methodName +
                " 参数 " + Arrays.asList(arguments) +
                " 将被执行");
        Object returnedByMethod = joinPoint.proceed();
        logger.info("方法执行并返回 " + returnedByMethod);
        return returnedByMethod;
    }
}

这是我的配置类->

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;
    }
}

这是我希望拦截方法的类->

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("发布评论:" + comment.getText());
        return "成功";
    }
}

我想通过Spring中的切面拦截方法来查看日志,但我无法看到日志。

英文:

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

答案1

得分: 1

只需在您的 ComponentConfig 类上添加 @EnableAspectJAutoProxy 注解,拦截功能就会生效。

英文:

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

huangapple
  • 本文由 发表于 2023年8月11日 02:33:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76878468.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定