简体   繁体   中英

Spring AOP aspect does not intercept annotated method

i want to use the @before , @after and @AfterThrowing in my function. If other function use the annotation like @MyAspectTest , it should run beforeAction() , afterAction() and afterExcept() at related time. But, it seems doesn't work.

i have already input the dependency and modified the beans.

package com.service.metrics;

import com.mgr.CMPMgr;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

import org.aspectj.lang.annotation.AfterThrowing;

import java.lang.reflect.Method;

@Aspect
public class CMPAspect {
    @Before(value="@annotation(com.mgr.CMPMgr)")
    public void beforeAction(JoinPoint joinPoint) throws ClassNotFoundException {
      testcode
    }

    @After(value="@annotation(com.mgr.CMPMgr)")
    public void afterAction(){
      testcode
    }

    @AfterThrowing(value="@annotation(com.mgr.CMPMgr)")
    public void afterExcept(){
      testcode
    }
}
package com.mgr;

public @interface CMPMgr {
    String name() default "";
    long startTime = System.currentTimeMillis();
}
    @CMPMgr(name = "vipGet")
    @GET
    @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
    @Path("/{lbId}")
    public Response get(@PathParam("lbId")String lbId,
                        @HeaderParam("Authorization") String basicAuthData,
                        @HeaderParam("UserID") String behalf) {
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.0</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.0</version>
        </dependency>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:task="http://www.springframework.org/schema/task"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    <aop:aspectj-autoproxy />
    <!-- enabling annotation driven configuration / -->
    <context:annotation-config />
    <context:component-scan />

    <!-- responsible for registering the necessary Spring components that power
        annotation-driven transaction management; such as when @Transactional methods
        are invoked -->
    <tx:annotation-driven />

it should get into the Aspect functions. But i don't find it work in debug mode. Why?

Some things come to mind:

  • Your annotation needs runtime retention, but I do not see @Retention(RetentionPolicy.RUNTIME) in your code.
  • Your aspect should be a @Component , but I do not see the corresponding annotation either.
  • The target class containing method public Response get(..) also has to be a Spring bean/component. Because you only show incoherent snippets instead of full class definitions, I have no idea which package that class resides in, if it is a Spring component and whether or not it is picked up by component scan.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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