无法识别Java注释

时间:2019-10-24 09:55:58

标签: java spring spring-boot annotations spring-tool-suite

我有一个使用@PreAuthorize进行安全检查的spring boot应用程序。如果我通过执行gradle任务bootRun启动验证,则验证在我的应用程序中有效。如果我从Spring Tool Suite(使用标准运行配置)启动应用程序,那么安全检查将无法进行。

我认为问题是从STS开始时无法识别PreAuthorize批注。我可以使用与我的实际应用程序相同的继承层次结构,通过一个简单的gradle spring boot应用程序来重现它。

  • “ DemoEntity”扩展了“ BaseDemo”,后者扩展了“ AbstractBaseEntity”。
  • “ DemoService”扩展了实现“ IEntityService”的“ AbstractService”
  • “ DemoService”实现了扩展“ IEntityService”的“ IDemoService”。

以下是相关代码:

public class DemoEntity extends BaseDemo {}
public class BaseDemo extends AbstractBaseEntity {}
public abstract class AbstractBaseEntity {}

public class DemoService extends AbstractService<BaseDemo, DemoEntity> implements IDemoService {
    @Override
    @DemoAnnotation
    public void create(DemoEntity entity) {}
}

public interface IDemoService extends IEntityService<DemoEntity> {}

abstract class AbstractService<B extends AbstractBaseEntity, E extends B> implements IEntityService<E> {
    @Override
    public void create(E entity) {
        throw new UnsupportedOperationException();
    }
}

public interface IEntityService<E> {
    void create(E entity);
}

@Retention(RetentionPolicy.RUNTIME)
public @interface DemoAnnotation {}

注意:

  • 我在DemoService类上注释了“ create”方法。
  • 此方法重写AbstractService的“ create”方法,并实现在IEntityService上声明的方法。

我用以下代码打印方法及其注释。

@SpringBootApplication
public class AnnotationDemoApplication {

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

        for (final Method method : DemoService.class.getMethods()) {
            if (method.getName().contains("create")) {
                System.out.println("Method: " + method.toString());
                final Annotation[] annotations = method.getAnnotations();
                System.out.println("  #Annotations: " + annotations.length);
                for (final Annotation annotation : annotations) {
                    System.out.println("  Annotation: " + annotation.toString());
                }
                System.out.println();
            }
        }
    }
}

从gradle任务bootRun开始时,我得到以下结果:

Method: public void com.example.annotationdemo.DemoService.create(java.lang.Object)
  #Annotations: 1
  Annotation: @com.example.annotationdemo.DemoAnnotation()

Method: public void com.example.annotationdemo.DemoService.create(com.example.annotationdemo.AbstractBaseEntity)
  #Annotations: 1
  Annotation: @com.example.annotationdemo.DemoAnnotation()

Method: public void com.example.annotationdemo.DemoService.create(com.example.annotationdemo.DemoEntity)
  #Annotations: 1
  Annotation: @com.example.annotationdemo.DemoAnnotation()

在继承层次结构中,每个方法都有注释。 从STS启动应用程序时,结果为:

Method: public void com.example.annotationdemo.DemoService.create(com.example.annotationdemo.DemoEntity)
  #Annotations: 1
  Annotation: @com.example.annotationdemo.DemoAnnotation()

Method: public void com.example.annotationdemo.DemoService.create(com.example.annotationdemo.AbstractBaseEntity)
  #Annotations: 0

Method: public void com.example.annotationdemo.AbstractService.create(java.lang.Object)
  #Annotations: 0

我认为这个演示在真实的应用程序中再现了我的问题的原因。 我是使用Spring Boot 2.2.0,Java 11和没有其他依赖项的spring initializr创建的项目。

有人可以告诉我,为什么STS除了运行bootRun还会产生其他结果? 我是否需要在STS中添加参数或设置,使其行为与bootRun相同?

0 个答案:

没有答案