简体   繁体   English

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

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

The annotation seems to have no affect. 注释似乎没有影响。 Added more text here to satisfy the editor that this site has requiring a certain amount of verbosity. 在此处添加了更多文本,以满足编辑者的要求,该网站需要一定的详细程度。

My Pom entry 我的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>

In applicationContext.xml (where other beans are defined) 在applicationContext.xml中(在其中定义了其他bean)

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

My aspect 我的方面

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

The annotation 注释

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

THIS CODE IS INSIDE OF MY CONTROLLER 此代码位于我的控制器内

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

Have you made sure both the location you are defining the annotation in is component scanned and that the location you are using it in is component scanned? 您是否已确定对定义注释的位置进行了组件扫描,并确保对使用注释的位置进行了组件扫描?

Also, I'm not sure if this matters; 另外,我不确定这是否重要; but I've generally seen people/examples put full package qualifiers in @around (on LogArguments in this case). 但是我通常看到人们/示例在@around中放置了完整的包限定符(在这种情况下是LogArguments)。

Changes to Aspect 方面的变化

  • Remove @Component annotation 删除@Component批注
  • Modify the @Around annotation and the logArguments method signature to make it work. 修改@Around注释和logArguments方法签名,使其工作。 The below example should work, 下面的示例应该可以工作,

     @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(); } } 

Changes to applicationContext.xml 对applicationContext.xml的更改

  • Make sure to add <aop:aspectj-autoproxy /> 确保添加<aop:aspectj-autoproxy />
  • I don't think you need to specify lazy-init 我认为您不需要指定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