【问题标题】:Aspect not getting invoked方面没有被调用
【发布时间】:2020-07-08 10:35:56
【问题描述】:

我正在做一个简单的项目,我想在其中使用方面。我创建了一个包含我的方面的 jar(代码如下)

package com.example.aop.logging.aspect;

import java.util.Arrays;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAspect {

    @Before("@annotation(com.example.aop.logging.aspect.Loggable)")
    public void before(JoinPoint joinPoint) {
        System.out.print("***********Before************" + joinPoint.getSignature().getName() + " called with param -> "
                + Arrays.toString(joinPoint.getArgs()));
    }
}

下面是我的 Loggable 注释

package com.example.aop.logging.aspect;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Loggable {

}

我想将此 jar 包含在另一个具有我的 api 的 spring boot 项目中

package com.example.server.controller;

import org.apache.log4j.Logger;

import com.example.aop.logging.aspect.Loggable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CreateController {

    private static final Logger LOGGER = Logger.getLogger(CreateController.class);
    
    @Loggable
    @CrossOrigin(origins = "*", allowedHeaders = "*")
    @PostMapping("/create/{userId}")
    public Topic create(@PathVariable String userId) {
        
        LOGGER.debug("Inside createTopic -3 :::" );     
    }   
}

方面没有被调用,你能帮忙

【问题讨论】:

  • 我的建议,不要那样做,创建自定义注释来标记您的连接点。除非您想为您创建的每个连接点添加配置
  • 还有:如何通过控制器将“MyAspect”链接到库?
  • 您的配置中有@EnableAspectJAutoProxy 吗?
  • 我已将 jar(包含 MyAspect)作为依赖项添加到我的 api。其次,你能帮我举个例子来说明你的提议吗
  • @zlaval 我将@EnableAspectJAutoProxy 添加到 MyAspect 类中,仍然没有成功

标签: java spring spring-boot annotations spring-aop


【解决方案1】:

感谢 R.G.问题是组件扫描

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-13
    • 2014-06-02
    • 2011-08-28
    • 2017-02-24
    • 2015-02-23
    • 2013-12-19
    • 1970-01-01
    相关资源
    最近更新 更多