EN
Spring Boot 2 - set full class names as bean names (package + class name)
10 points
In this short article, we would like to show how to set full class names as bean names in Spring Boot 2.
Note: as full class name we understand
some.package.ClassName
format as way to name beans.
Quick solution:
Use
nameGenerator
parameter in@SpringBootApplication
annotation.
xxxxxxxxxx
1
nameGenerator = MyBeanNameGenerator.class) (
SpringBootWebApplication.java
file:
xxxxxxxxxx
1
package com.dirask.example;
2
3
import org.springframework.boot.SpringApplication;
4
import org.springframework.boot.autoconfigure.SpringBootApplication;
5
6
nameGenerator = CustomBeanNameGenerator.class) // <----- REQUIRED (
7
public class SpringBootWebApplication {
8
9
public static void main(String[] args) throws Exception {
10
SpringApplication.run(SpringBootWebApplication.class, args);
11
}
12
}
CustomBeanNameGenerator.java
file:
xxxxxxxxxx
1
package com.dirask.example;
2
3
import org.springframework.beans.factory.config.BeanDefinition;
4
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
5
import org.springframework.beans.factory.support.BeanNameGenerator;
6
7
public class CustomBeanNameGenerator implements BeanNameGenerator {
8
9
10
public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
11
return definition.getBeanClassName();
12
}
13
}