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

时间:2017-10-11 16:15:09

标签: java spring annotations aop

注释似乎没有任何影响。在这里添加了更多文本,以满足编辑本网站需要一定程度的冗长。

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

2 个答案:

答案 0 :(得分:0)

您是否确定要定义注释的位置是否已扫描组件,以及您使用它的位置是否已扫描组件?

另外,我不确定这是否重要;但我一般都看到人/示例在@around中放入了完整的包限定符(在本例中为LogArguments)。

答案 1 :(得分:0)

Aspect的更改

  • 删除@Component注释
  • 修改<div class="wrapper"> <a href="#"> <img src="http://via.placeholder.com/250x150" /> </a> </div>注释和@Around方法签名以使其正常工作。以下示例应该有效,

    logArguments

对applicationContext.xml的更改

  • 请务必添加@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(); } }
  • 我认为您不需要指定<aop:aspectj-autoproxy />

    lazy-init