繁体   English   中英

Spring AOP自定义注释未为我的控制器触发

[英]Spring AOP Custom annotation not firing for my controller

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

我的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);
    }

您是否已确定对定义注释的位置进行了组件扫描,并确保对使用注释的位置进行了组件扫描?

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

方面的变化

  • 删除@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的更改

  • 确保添加<aop:aspectj-autoproxy />
  • 我认为您不需要指定lazy-init

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM