Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are beans named by default when created with annotation?

I am working with Spring java code written by someone else. I want to reference a bean that's created by annotation (of field classABC):

@Component
public class ClassService
{
    @Autowired
    ClassABC classABC;

public interface ClassABC

@Repository
public class ClassABCImpl extends BaseABC implements ClassABC

The following code tries to get a reference to the ClassABC bean by name, but does not work:

ClassABC classABC = ApplicationContext.getBean("classABC");

However, the following code that references this bean by type does work:

ClassABC classABC = ApplicationContext.getBean(ClassABC.class);

Because the first reference does not work, I am guessing that the bean is not named "classABC". What is the name of this bean?

Note: there is no configuration xml that references this bean, so I don't think the bean name is defined in xml.

like image 831
DSimkin Avatar asked Feb 19 '18 18:02

DSimkin


People also ask

What is the default name of bean?

According to the CDI specification, the default name for a managed bean is the unqualified class name of the bean class, after converting the first character to lower case. For example, if the bean class is named ProductList, the default bean name is productList.

How does Spring generate bean names for classes annotated with @component that do not specify a name?

For stereotype annotation based bean, if the name is not explicitly specified with the value field of stereotype annotations, then the name is again generated by AnnotationBeanNameGenerator which is an implementation of the BeanNameGenerator strategy interface, the names generated is simply the short name of the class, ...

What does @bean annotation mean?

One of the most important annotations in spring is the @Bean annotation which is applied on a method to specify that it returns a bean to be managed by Spring context. Spring Bean annotation is usually declared in Configuration classes methods. This annotation is also a part of the spring core framework.

Which annotation is used to configure the beans automatically?

The @Autowired annotation can apply to bean property setter methods, non-setter methods, constructor and properties.


2 Answers

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/AnnotationBeanNameGenerator.html

Is the default bean name generator for annotations, there's a DefaultBeanNameGenerator for beans defined by @Bean

In this case I believe the name of the bean would be classABCImpl, as its built of the short name of the class.

From the example of a concrete service implementation, com.xyz.FooServiceImpl -> fooServiceImpl

Personally I'm not a fan of using the default naming if you are ever going to want to refer to that bean via the name. Better to be explicit in these cases.

like image 80
Darren Forsythe Avatar answered Sep 30 '22 13:09

Darren Forsythe


Adding to Darren Forsythe's answer above, the implementation of AnnotationBeanNameGenerator can result in two kinds of bean names.

If the short name derived from the class name starts with two uppercase characters, the short name will become the bean name. Otherwise if one of the first two characters of short name is lower case, the bean name would be the short name with the first character in lower case.

Example: com.xyz.FooServiceImpl class name leads to a short name of FooServiceImpl. Since, first two characters are not uppercase, the bean name would become -> fooServiceImpl.

However, if class name is com.xyz.FOoServiceImpl which leads to a short name of FOoServiceImpl, your bean name would remain FOoServiceImpl since the first two characters are uppercase.

Similarly, com.xyz.FOoServiceIMPL, will lead to bean name as FOoServiceIMPL.

Spring only looks at the short name and its first two characters while creating the bean name.

like image 25
Priyank Agrawal Avatar answered Sep 30 '22 12:09

Priyank Agrawal