Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.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 如何为Spring Boot应用程序处理@AfterReturning方面的自定义对象_Java_Spring_Aop_Spring Aop_Aspect - Fatal编程技术网

Java 如何为Spring Boot应用程序处理@AfterReturning方面的自定义对象

Java 如何为Spring Boot应用程序处理@AfterReturning方面的自定义对象,java,spring,aop,spring-aop,aspect,Java,Spring,Aop,Spring Aop,Aspect,所以,我有下一节课: public class MyCustomClass { private String someField; public String getSomeField() { return someField; } public void setSomeField(String someField) { this.someField = someField; } } 我想处理返回此类实例的方法: @

所以,我有下一节课:

public class MyCustomClass {

    private String someField;

    public String getSomeField() {
        return someField;
    }

    public void setSomeField(String someField) {
        this.someField = someField;
    }
}
我想处理返回此类实例的方法:

@Aspect
public class CustomAspect {
    @AfterReturning(
            pointcut = "execution(* com.*.*(..))",
            returning = "val")
    public void handleCustom(JoinPoint joinPoint, MyCustomClass val) {
        System.out.println(val.getSomeField());
    }

}
但只有当val的类型为Object时,它才起作用:

 public void handleCustom(JoinPoint joinPoint, Object val) {//...}
我试着做这样的东西:

  if (val instanceof MyCustomClass) {
        System.out.println(((MyCustomClass) val).getSomeField());
    }
但它不工作,在调试模式下,我看到类型是ArrayList,它是空的

我知道,我是AOP的初学者,但是你能建议一些可以帮助我的解决方法吗

更新:

Maven依赖项:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
            <version>1.4.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.9</version>
        </dependency>
我也尝试将CustomAspect用作@组件

自定义对象只是一个DTO,它取自maven工件,并通过rest请求获得

因此,如果val的类型是Object,那么我没有问题,一切都正常。但是我确实希望有MyCustomClass而不是Object。可能吗

更新2:

@RestController
@RequestMapping(value = "/api/v1", produces = MediaType.APPLICATION_JSON_VALUE)
public class MyController{
    @Autowired
    private MyService myService;

    @RequestMapping(value = "dosmth")
    public ResponseEntity<?> doSmth() {
        SomeObject res = myService.doSmthAndGet();
        return new ResponseEntity<>(res, HttpStatus.OK);
    }

}



@Service
public class MyService{
   public SomeObject doSmthAndGet() {
      return SomeObject.of(getMyCustomClass());
   }

   private MyCustomClass getMyCustomClass() {
      MyCustomClass result = new MyCustomClass();
      result.setSomeField("Hello, Stack!");
      return result;
   }


}
它仅由从控制器调用的方法触发:

 public SomeObject doSmthAndGet()
我尝试对@AfterReturning使用不同的执行方式。。。
我做错了什么?

使用AspectJ。您使用的是Spring AOP而不是AspectJ吗?您能说明您的建议是如何应用的吗?顺便说一句@AfterReturning不需要jointpoint参数代码应该工作,为您的
@Aspect
声明
@组件
,并确保它已被Spring扫描。我假设
@EnableAspectJAutoProxy
已声明,或者通过
Spring Boot
已通过依赖项检测在内部启用。我提供了maven依赖项。注释取自org.aspectj.lang.annotation package@ManuelJordan,我尝试使用组件或Bean声明,结果是使用返回
MyCustomClass
对象的方法发布类声明的相同结果?我的意思是,
AOP
必须拦截的预期对象/类与AspectJ一起工作。您使用的是Spring AOP而不是AspectJ吗?您能说明您的建议是如何应用的吗?顺便说一句@AfterReturning不需要jointpoint参数代码应该工作,为您的
@Aspect
声明
@组件
,并确保它已被Spring扫描。我假设
@EnableAspectJAutoProxy
已声明,或者通过
Spring Boot
已通过依赖项检测在内部启用。我提供了maven依赖项。注释取自org.aspectj.lang.annotation package@ManuelJordan,我尝试使用组件或Bean声明,结果是使用返回
MyCustomClass
对象的方法发布类声明的相同结果?我是指必须被
AOP
private MyCustomClass getMyCustomClass()
 public SomeObject doSmthAndGet()