Consistent naming of beans is important for the readability and maintainability of the code. More precisely, according to the Spring documentation:
Naming beans consistently makes your configuration easier to read and understand. Also, if you use Spring AOP, it helps a lot when applying advice to a set of beans related by name.
Not following accepted conventions can introduce inconsistent naming, especially when multiple developers work on the same project, leading to technical debt.
The spring documentation establishes a naming convention that consists of camel-cased names with a leading lowercase letter.
This rule raises an issue when a bean name defined in one of the following annotations does not adhere to the naming convention:
@Bean @Configuration @Controller @Component @Qualifier @Repository @Service Change the bean’s name to adhere to the naming conventions. Names should be camel-cased and start with a lowercase letter, for example,
myBean.
@Bean(name = "MyBean") // Noncompliant, the first letter of the name should be lowercase
public MyBean myBean() {
...
@Bean(name = "myBean") // Compliant
public MyBean myBean() {
...
@Service("my_service") // Noncompliant, the name should be camel-cased
public class MyService {
...
@Service("myService") // Compliant
public class MyService {
...