【问题标题】:Spring AOP Custom annotation not firing for my controllerSpring AOP 自定义注释没有为我的控制器触发
【发布时间】:2017-11-07 13:27:11
【问题描述】:

注释似乎没有影响。在此处添加了更多文本以满足编辑器,该站点需要一定的详细程度。

我的 Pom 条目

<dependency> 
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-api</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>jcl-over-slf4j</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>jul-to-slf4j</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>log4j-over-slf4j</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>logback-classic</artifactId>
                    <groupId>ch.qos.logback</groupId>
                </exclusion>
            </exclusions>
        </dependency>

在 applicationContext.xml 中(定义了其他 bean)

<bean id="myAspect" class="com.myapp.MyAspect" lazy-init="false"/>

我的一面

 package com.myapp;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.springframework.stereotype.Component;
    @Aspect
    @Component
    public class MyAspect {
        @Around("@annotation(LogArguments)")
        public Object logArguments(ProceedingJoinPoint joinPoint) throws Throwable {
            System.err.println("put breakpoint here, never stops here");
            return joinPoint.proceed();
        }
    }

注释

package com.myapp;
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 LogArguments {
}

此代码在我的控制器内部

@RequestMapping(value = "/search", method = RequestMethod.POST)
    @LogArguments
    public @ResponseBody SearchResult performSearch(@RequestBody SearchForm 
    searchForm, HttpServletRequest request) throws Exception {
    LOG.debug("If I put a break point here it stops here, but not in the aspects code:" + searchForm);
    }

【问题讨论】:

    标签: java spring annotations aop


    【解决方案1】:

    方面的变化

    • 移除@Component注解
    • 修改@Around注解和logArguments方法签名使其生效。下面的例子应该可以工作,

      @Aspect
      public class MyAspect {
      
          @Around("@annotation(annotation) || @within(annotation)")
          public Object logArguments(ProceedingJoinPoint joinPoint,
              LogArguments annotation) throws Throwable {
              System.out.println("put breakpoint here, never stops here");
      
              return joinPoint.proceed();
          }
      }
      

    对 applicationContext.xml 的更改

    • 确保添加&lt;aop:aspectj-autoproxy /&gt;
    • 我认为您不需要指定lazy-init

      <aop:aspectj-autoproxy />
      
      <bean id="myAspect" class="com.myapp.MyAspect"/>
      

    【讨论】:

    • 所以补充说,现在看起来像`@Around("@annotation(annotation) || @within(annotation)") public Object logArguments(ProceedingJoinPoint joinPoint, LogArguments annotation) throws Throwable {... ` 仍然没有停止在方法中
    • 无法弄清楚添加代码的该死语法,对不起,无论如何做了你推荐的,没有工作。
    • @WarrenGoldman 更新了我的帖子。请查看对applicationContext.xml 的更改并删除了@Component 注释。我用 Java 配置测试了 Aspect 类,它工作正常。
    【解决方案2】:

    您是否确保定义注释的位置是组件扫描的,并且您使用它的位置是组件扫描的?

    另外,我不确定这是否重要;但我通常看到人们/示例将完整的包限定符放在 @around 中(在这种情况下是 LogArguments)。

    【讨论】:

    • 注释不在组件扫描中,是否需要?
    • 扫描我使用的地方
    • 啊,好的;我想那不是你的问题。我通常不会添加新注释,但我会在控制器方法上横切日志记录,如下所示:@Around("execution(* *(..)) && @annotation(org.springframework.web.bind.annotation.RequestMapping )”)。没有一个很好的基于注释的示例来快速测试,抱歉。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多