我无法找到 spring bean 的类。我目前拥有的唯一信息是 bean id。
在我的应用程序上下文中,我有以下 bean:
<bean id="someConfig" class="com.foo.bar.GenericConfig">
我目前只有类“someConfig”的名称。如何从 bean 名称获取 Class?有没有办法在不使用 ApplicationContext.getBean() 的情况下执行此操作?
理想情况下,我希望能够做这样的事情:
Class<?> clazz = getClass("someConfig");
最佳答案
正如您在 javadoc 中看到的那样有一个方法 getType
返回该类。
getType Class getType(String name) throws NoSuchBeanDefinitionException
Determine the type of the bean with the given name. More specifically, determine the type of object that getBean(java.lang.String) would return for the given name. For a FactoryBean, return the type of object that the FactoryBean creates, as exposed by FactoryBean.getObjectType().
Translates aliases back to the corresponding canonical bean name. Will ask the parent factory if the bean cannot be found in this factory instance.
Parameters:
name - the name of the bean to query
Returns:
the type of the bean, or null if not determinable
Throws:
NoSuchBeanDefinitionException - if there is no bean with the given name
Since:
1.1.2
See Also:
getBean(java.lang.String), isTypeMatch(java.lang.String, org.springframework.core.ResolvableType)
关于java - 从spring bean名称获取类对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39213716/