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:
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.
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");
}
}
One of the most important annotations in spring is @ComponentScan which is used along with the @Configuration annotation to specify the packages that we want to be scanned. @ComponentScan without arguments tells Spring to scan the current package and all of its sub-packages.
@Component and @ComponentScan are for different purposes. @Component indicates that a class might be a candidate for creating a bean. It's like putting a hand up. @ComponentScan is searching packages for Components.
@ComponentScan - to enable component scanning, all the packages and subpackages will be auto-scanned which are under the root package on which @SpringBootApplication is applied. @EnableAutoConfiguration - to enable auto-configuration of the. classes bases on the jars added in classpath.
@Component is a class-level annotation, but @Bean is at the method level, so @Component is only an option when a class's source code is editable. @Bean can always be used, but it's more verbose. @Component is compatible with Spring's auto-detection, but @Bean requires manual class instantiation.
With the help of Fabio Formosa's answer, the missing bits filled in from this answer, and some inspiration from @EntityScan
annotation, I finally managed to get this to work. A compilable, working example can be found at https://github.com/knittl/stackoverflow/tree/spring-enable-annotation-working.
In a nutshell: building on Fabio's answer, it is important to properly configure a ClassPathScanningCandidateComponentProvider
instance with include filters and then run it against all provided base classes. @AliasFor(annotation = Import.class, …)
does not seem to be required and can be aliased to another attribute, e.g. basePackages
of the same annotation.
The minimum implementation is as follows:
@Configuration
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(EnableCustom.EnableCustomConfiguration.class)
public @interface EnableCustom {
@AliasFor(attribute = "basePackages")
String[] value() default {};
@AliasFor(attribute = "value")
String[] basePackages() default {};
class EnableCustomConfiguration implements ImportBeanDefinitionRegistrar, EnvironmentAware {
private static final BeanNameGenerator BEAN_NAME_GENERATOR = AnnotationBeanNameGenerator.INSTANCE;
private Environment environment;
@Override
public void setEnvironment(final Environment environment) {
this.environment = environment;
}
@Override
public void registerBeanDefinitions(
final AnnotationMetadata metadata,
final BeanDefinitionRegistry registry) {
final AnnotationAttributes annotationAttributes = new AnnotationAttributes(
metadata.getAnnotationAttributes(EnableCustom.class.getCanonicalName()));
final ClassPathScanningCandidateComponentProvider provider
= new ClassPathScanningCandidateComponentProvider(false, environment);
provider.addIncludeFilter(new AnnotationTypeFilter(MyAnnotation.class, true));
final Set<String> basePackages
= getBasePackages((StandardAnnotationMetadata) metadata, annotationAttributes);
for (final String basePackage : basePackages) {
for (final BeanDefinition beanDefinition : provider.findCandidateComponents(basePackage)) {
final String beanClassName = BEAN_NAME_GENERATOR.generateBeanName(beanDefinition, registry);
if (!registry.containsBeanDefinition(beanClassName)) {
registry.registerBeanDefinition(beanClassName, beanDefinition);
}
}
}
}
private static Set<String> getBasePackages(
final StandardAnnotationMetadata metadata,
final AnnotationAttributes attributes) {
final String[] basePackages = attributes.getStringArray("basePackages");
final Set<String> packagesToScan = new LinkedHashSet<>(Arrays.asList(basePackages));
if (packagesToScan.isEmpty()) {
// If value attribute is not set, fallback to the package of the annotated class
return Collections.singleton(metadata.getIntrospectedClass().getPackage().getName());
}
return packagesToScan;
}
}
}
Use a custom annotation @EnableAnnotation with basePackages attribute
@EnableAnnotation(basePackages = "write-here-a-base-package")
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class SampleSimpleApplication implements CommandLineRunner {
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleSimpleApplication.class, args);
}
}
@EnableAnnotation is defined so:
@Retention(RUNTIME)
@Target(TYPE)
@Import(EnableAnnotationConfigRegistrar.class)
public @interface EnableAnnotation {
String[] basePackages() default "*";
@AliasFor(annotation = Import.class, attribute = "value")
Class<?>[] value() default { EnableAnnotationConfigRegistrar.class };
}
Finally, the EnableAnnotationConfigRegistrar.class scans programmatically:
public class EnableAnnotationConfigRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata enableAnnotationMetadata,
BeanDefinitionRegistry registry) {
AnnotationAttributes enableAnnotationAttributes = new AnnotationAttributes(
enableAnnotationMetadata.getAnnotationAttributes(EnableAnnotation.class.getName()));
String[] basePackages = enableAnnotationAttributes.getStringArray("basePackages");
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(basePackages);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With