Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Component scan using custom annotation

Tags:

I am consuming a spring boot project as jar inside another spring boot application using the maven dependency. I want to do the component scan of jar only if I enable a custom annotation from microservice.

@SpringBootApplication
//@ComponentScan({"com.jwt.security.*"})  To be removed by custom annotation
@MyCustomAnnotation  //If I provide this annotation then the security configuration of the jar should be enabled.
public class MicroserviceApplication1 {

    public static void main(String[] args) throws Exception {

        SpringApplication.run(MicroserviceApplication1.class, args);

    }

}

Please suggest some ideas.

like image 574
Ravi Avatar asked Nov 30 '17 04:11

Ravi


1 Answers

In your library:

@Configuration
@ComponentScan(basePackages = { "com.jwt.security" })
public class MyCustomLibConfig{

}


@Retention(RUNTIME)
@Target(TYPE)
@Import(MyCustomLibConfig.class)
public @interface MyCustomAnnotation{

 @AliasFor(annotation = Import.class, attribute = "value")
           Class<?>[] value() default { MyCustomLibConfig.class };
}

So, in your application you can use the annotation

@SpringBootApplication
@MyCustomAnnotation  //If I provide this annotation then the security configuration 
                       of the jar should be enabled.
public class MicroserviceApplication1 {

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

}
like image 55
Fabio Formosa Avatar answered Sep 23 '22 13:09

Fabio Formosa