72 lines
2.0 KiB
Plaintext
72 lines
2.0 KiB
Plaintext
== 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. 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`
|
|
|
|
== 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
|
|
|
|
[source,java,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
@Bean(name = "MyBean") // Noncompliant, the first letter of the name should be lowercase
|
|
public MyBean myBean() {
|
|
...
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,java,diff-id=1,diff-type=compliant]
|
|
----
|
|
@Bean(name = "myBean") // Compliant
|
|
public MyBean myBean() {
|
|
...
|
|
----
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,java,diff-id=2,diff-type=noncompliant]
|
|
----
|
|
@Service("my_service") // Noncompliant, the name should be camel-cased
|
|
public class MyService {
|
|
...
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,java,diff-id=2,diff-type=compliant]
|
|
----
|
|
@Service("myService") // Compliant
|
|
public class MyService {
|
|
...
|
|
----
|
|
|
|
== Resources
|
|
|
|
=== Documentation
|
|
* Spring Framework Documentation - https://docs.spring.io/spring-framework/reference/core/beans/definition.html#beans-beanname[3.3 Bean overview]
|
|
|
|
=== Articles & blog posts
|
|
* Java Guides - https://www.javaguides.net/2019/03/spring-boot-best-practices.html[Spring Boot Best Practices]
|