Spring bean definition - get bean class
spring, spring-bean, web-applications
Solution
You can't do this after creating the `ApplicationContext`. Most `ApplicationContext` implementations will `refresh()` themselves and force instantiation of the beans.
What you can do is create a `BeanFactoryPostProcessor` in which you get the target bean definition and check the bean class.
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
String className = beanFactory.getBeanDefinition("").getBeanClassName();
}
But note, as the javadoc for `getBeanClassName()` states
Hence, do not consider this to be the definitive bean type at runtime but rather only use it for parsing purposes at the individual bean definition level.
So use it with a grain of salt.
If you give us more details as to what you are trying to accomplish, there might be alternatives.
Problem
I'm trying the get Bean class name without initialize the bean. I need to know the class , I could get the bean from applicationContext and to check the class name from the bean instance, But I want to know the class with out actually creating/init the bean.. Is it possible ? ``` Object bean = applicationContext.getBean("beanName"); bean.getClass(); ```