Junior — Middle
What happens to the Spring bean name if you do not explicitly specify a name or identifier when declaring it?
sobes.tech AI
Answer from AI
If you do not explicitly specify a name or identifier when declaring a bean in Spring, Spring will automatically generate a bean name based on the class name.
Naming rules:
- The class name with a lowercase letter at the beginning (camelCase).
- For example, if the class is named
MyService, the bean name will bemyService.
This allows you to refer to the bean by this name in the Spring context if no other name is specified.
Example:
@Component
public class UserRepository {
// the bean will be accessible under the name "userRepository"
}
Thus, explicitly specifying a name is not necessary if you are satisfied with the default name based on the class name.