[英]AOP spring aspect not triggered
Hi i have a problem with an Aspect that is not triggered and i don't know why. 嗨,我遇到了一个未触发的方面问题,我也不知道为什么。 Here is how it is my project:
这是我的项目:
The annotation: 注释:
import java.lang.annotation.*;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Profiled {
}
The aspect: 方面:
import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class ProfiledAspect {
final static Logger logger = Logger.getLogger(ProfiledAspect.class);
private final String TIME_UNIT = "ms";
@Around("@annotation(annotation)")
public Object profile(ProceedingJoinPoint pjp, Profiled annotation) throws Throwable {
long start = System.nanoTime();
Object result = pjp.proceed();
long end= System.nanoTime();
long duration = end - start;
System.out.println("Duration: " + duration + "nano secs.");
return result;
}
}
Here is my config class: 这是我的配置类:
import com.myexample.aop.ProfiledAspect;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan(basePackages = {"com.myexample"})
@EnableAspectJAutoProxy
public class AppConfig {
@Bean
public ProfiledAspect profiledAspect(){
return new ProfiledAspect();
}
}
Here is my class where i am using the annotation to trigger the aspect: 这是我在课堂上使用注解触发方面的地方:
import com.myexample.aop.Profiled;
import org.springframework.stereotype.Service;
@Service
public class TestAnnotationClass {
@Profiled
public void testAnnotationClass() {
System.out.println("test profile annotation");
}
}
Here are the maven dependency that i am using for the AOP thing 这是我用于AOP的Maven依赖项
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.10</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
Any idea why the aspect is not triggered? 知道为什么不触发方面吗?
Is the Profiled Annotation missing in the @Around @Around中是否缺少分析注释
@Around("@annotation(com.example.Profiled)")
public Object profile(ProceedingJoinPoint pjp, Profiled annotation) throws Throwable {
...
}
You can make the aspect also a @Component
and have it picked up by component scan instead of instantiating it manually. 您可以将方面也
@Component
并通过组件扫描将其拾取,而不是手动实例化它。
Another idea is to use @EnableAspectJAutoProxy(proxyTargetClass=true)
because your target class does not implement an interface which could be proxied by a JDK proxy, but a simple class. 另一个想法是使用
@EnableAspectJAutoProxy(proxyTargetClass=true)
因为您的目标类没有实现一个可以由JDK代理代理的接口,而是一个简单的类。 For that you need a CGLIB proxy. 为此,您需要一个CGLIB代理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.