Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I set a default AnnotationBeanNameGenerator in Spring?

Tags:

java

spring

I'm using @ComponentScan multiple places in the test suite for my application. I want to replace the default bean name generation in Spring, so I have made my own generator like this:

public class FullyQualifiedAnnotationBeanNameGenerator extends AnnotationBeanNameGenerator {
    protected String buildDefaultBeanName(BeanDefinition definition) {
        return definition.getBeanClassName();
    }
}

To use the generator, I'm doing this:

@ComponentScan(nameGenerator = FullyQualifiedAnnotationBeanNameGenerator.class)

The problem is, for every @ComponentScan in my test configuration classes (classes annotated with @Configuration), I have to repeat nameGenerator = FullyQualifiedAnnotationBeanNameGenerator.class.

Is it possible for me to tell Spring to always use the FullyQualifiedAnnotationBeanNameGenerator as a nameGenerator? I want it to be the default one.

like image 809
Yngvar Kristiansen Avatar asked Oct 20 '25 14:10

Yngvar Kristiansen


1 Answers

Just create you own Annotation like this:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@ComponentScan(nameGenerator = FullyQualifiedAnnotationBeanNameGenerator.class)
public @interface MyComponentScan{

}
like image 79
dieter Avatar answered Oct 23 '25 08:10

dieter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!