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.
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.