Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java @使用注释时未调用方面_Java_Spring Boot_Annotations_Aspectj - Fatal编程技术网

Java @使用注释时未调用方面

Java @使用注释时未调用方面,java,spring-boot,annotations,aspectj,Java,Spring Boot,Annotations,Aspectj,我创建了一个注释以进行如下操作: @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.PARAMETER}) public @interface Verify { } 还有一个方面: @Aspect @Component public class VerifyAspect { @Before("execution(public * *(.., @Verify (*), ..))") public

我创建了一个注释以进行如下操作:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface Verify {
}
还有一个方面:

@Aspect
@Component
public class VerifyAspect {
    @Before("execution(public * *(.., @Verify (*), ..))")
    public void actionBefore(JoinPoint joinPoint) {
       System.out.println(joinPoint); // <<-------------- can't see this message
    }
}
但当我打电话时:

public void method(@Verify MyObject obj){
   // do something
}
这个方面根本没有被调用。我在创建过程中是否犯了任何错误?

来自:Spring AOP当前只支持方法执行连接点(建议在Spring Bean上执行方法)-因此请确保调用的方法是

public void method(@Verify MyObject obj){
   // do something
}
在一个SpringBean中声明

根据您共享的代码,我创建了一个简单的演示:

还要确保aspectjweaver.jar在您的依赖项中

pom.xml

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.6</version>
        </dependency>
配置

@SpringBootApplication
public class AspecjStyleAopApplication {

    public static void main(String[] args) {
        SpringApplication.run(AspecjStyleAopApplication.class, args);
    }

}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface Verify {
}
在这里,请确保为Spring提供了正确的basePackages来扫描组件

@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages = "com.example")
public class AspectJConfig {
}
注释

@SpringBootApplication
public class AspecjStyleAopApplication {

    public static void main(String[] args) {
        SpringApplication.run(AspecjStyleAopApplication.class, args);
    }

}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface Verify {
}
方面

@Aspect
@Component
public class VerifyAspect {

    @Before("execution(public * *(.., @Verify (*), ..))")
    public void actionBefore(JoinPoint joinPoint) {
        System.out.println("THIS SHOULD BE DISPLAYED");
        System.out.println(joinPoint); // <<-------------- can't see this message
    }
}
RestController

@RestController
public class SampleRestController {

    private final SampleService sampleService;

    public SampleRestController(SampleService sampleService) {
        this.sampleService = sampleService;
    }

    @GetMapping("/sample")
    public String sampleRestMethod() {
        sampleService.method(5);
        return "It works";
    }
}
当我调用
http://localhost:8080/sample
端点:

THIS SHOULD BE DISPLAYED
execution(void com.example.aspecjstyleaop.SampleService.method(Object))
Passed object: 5

第二行是您希望看到打印的内容。

是的,它是从一个带注释的类调用的
@Service
我也试图从
@RestController
调用它,但没有机会,您确定吗?我做了一些示例演示,对我来说效果很好。是的,我确定我有一个类似
@Service public class myservices{public void method(@Verify MyDto dto){System.out.println(“method”);}}
的方法,我从
@RestController
调用它,但它没有显示任何消息,这意味着它根本不需要方面。你能分享完整的例子吗?也许我忘了一些东西,或者一些配置谢谢你提供了完整的代码,但是我仍然无法使用它,你是否在pom.xml中导入了一些东西?