rspec/rules/S6862/java/rule.adoc

56 lines
1.6 KiB
Plaintext

== Why is this an issue?
Naming conventions play a crucial role in maintaining code clarity and readability.
The uniqueness of bean names in Spring configurations is vital to the clarity and readability of the code.
When two beans share the same name within a configuration, it is not obvious to the reader which bean is being referred to.
This leads to potential misunderstandings and errors.
== How to fix it
To address this issue, ensure each bean within a configuration has a distinct and meaningful name.
Choose names that accurately represent the purpose or functionality of the bean.
=== Code examples
==== Noncompliant code example
[source,java,diff-id=1,diff-type=noncompliant]
----
@Configuration
class Config {
@Bean
public User user() {
return currentUser();
}
@Bean
public User user(AuthService auth) { // Noncompliant
return auth.user();
}
}
----
==== Compliant solution
[source,java,diff-id=1,diff-type=compliant]
----
@Configuration
class Config {
@Bean
public User user() {
return currentUser();
}
@Bean
public User userFromAuth(AuthService auth) {
return auth.user();
}
}
----
== Resources
=== Documentation
* https://docs.spring.io/spring-framework/reference/core/beans/java/basic-concepts.html[Spring IO - Basic concepts: @Bean and @Configuration]
* https://docs.spring.io/spring-framework/reference/core/beans/java/configuration-annotation.html[Spring IO - Using the @Configuration annotation]
* https://docs.spring.io/spring-framework/reference/core/beans/java/bean-annotation.html[Spring IO - Using the @Bean annotation]