Why is this an issue?
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, and if you are using 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
How to fix it
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
.
Code examples
Noncompliant code example
@Bean(name = "MyBean") // Noncompliant, the first letter of the name should be lowercase
public MyBean myBean() {
...
Compliant solution
@Bean(name = "myBean") // Compliant
public MyBean myBean() {
...
Noncompliant code example
@Service("my_service") // Noncompliant, the name should be camel-cased
public class MyService {
...
Compliant solution
@Service("myService") // Compliant
public class MyService {
...
Resources
Documentation
Articles & blog posts