Spring custom @Enable annotation meta-annotated with @ComponentScan

旧巷老猫 提交于 2020-05-26 05:29:45

问题


I'm trying to write my own @Enable annotation for Spring framework, which should be used as follows:

package com.example.package.app;

@SpringBootApplication
@com.example.annotations.EnableCustom("com.example.package.custom")
public class MyApplication {}

I followed Component scan using custom annotation, but this poses several limitations:

  1. I cannot make the base package property dynamic, i.e. I cannot pass "com.example.package.base", but need to pre-define the package at the configuration.

    I had a look at @AliasFor, but cannot get it to work.

  2. When I leave out the base package, scanning starts from the defining package of the annotation, not from the package of the annotated class. In above's example, it would only scan and create beans for classes in com.example.annotations, but not for com.example.package.*.

    I had a look at EntityScanPackages.Registrar.class which is imported in @EntityScan annotation, but it is an internal class and my annotation cannot import.

Everything works if I put @ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, value = MyAnnotation.class)) on MyApplication class, but stops working when this is moved to a meta-annotation of @EnableCustom. How to tell Spring Framework to consider @EnableCustom as a different way of specifying @ComponentScan with some default values. I tried meta-annotating my annotation with @Configuration, @Component and others, to no avail:

@Configuration
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@ComponentScan(
        includeFilters = @ComponentScan.Filter(
                type = FilterType.ANNOTATION,
                value = ApplicationService.class))
public @interface EnableApplicationServices {
    @AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
    String[] value() default {};
}

Where can I find documentation for this or what starting point would you recommend? My long term goal is to have a Spring Boot starter which can be used by a multitude of projects.


A M(N)WE can be found in the following repository: https://github.com/knittl/stackoverflow/tree/spring-enable-annotation

Here's a rundown of the package structures:

// com.example.annotations.EnableCustom.java
@Configuration
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
// this annotation is never honored:
@ComponentScan(
        includeFilters = @ComponentScan.Filter(
                type = FilterType.ANNOTATION,
                value = MyAnnotation.class))
//@Import(EnableCustom.EnableCustomConfiguration.class)
public @interface EnableCustom {
    // this annotation works in combination with @Import, but scans the wrong packages.
    @ComponentScan(
            includeFilters = @ComponentScan.Filter(
                    type = FilterType.ANNOTATION,
                    value = MyAnnotation.class))
    class EnableCustomConfiguration {}
}

// file:com.example.app.Application.java
@SpringBootApplication
@EnableCustom("com.example.app.custom.services")
// @ComponentScan(
//         includeFilters = @ComponentScan.Filter(
//                 type = FilterType.ANNOTATION,
//                 value = MyAnnotation.class)) // <- this would work, but I want to move it to a custom annotation
public class Application {
}

// file:com.example.app.custom.services.MyService
@MyAnnotation
public class MyService {
    public MyService() {
        System.out.println("Look, I'm a bean now!");
    }
}

// file:com.example.annotations.services.WrongService.java
@MyAnnotation
public class WrongService {
    public WrongService() {
        System.out.println("I'm in the wrong package, I must not be instantiated");
    }
}

来源:https://stackoverflow.com/questions/61971497/spring-custom-enable-annotation-meta-annotated-with-componentscan

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!