简体   繁体   English

从Spring bean名称获取类对象

[英]Get class object from spring bean name

I'm having trouble finding the Class of a spring bean. 我在查找Spring bean的类时遇到了麻烦。 The only information I currently have is the bean id. 我当前拥有的唯一信息是bean id。

Given in my application context I have the following bean: 在我的应用程序上下文中给出的我有以下bean:

 <bean id="someConfig" class="com.foo.bar.GenericConfig">

I currently only have the name of the class "someConfig". 我目前只有类“ someConfig”的名称。 How do I obtain the Class from the bean name? 如何从Bean名称获取类? Is there any way of doing this without using ApplicationContext.getBean()? 没有使用ApplicationContext.getBean(),有没有办法做到这一点?

Ideally I want to be able to do something like this: 理想情况下,我希望能够执行以下操作:

Class<?> clazz = getClass("someConfig");

As you can see in the javadoc there is a method getType which returns the class. 如您在Javadoc中所见,有一个getType方法返回该类。

getType Class getType(String name) throws NoSuchBeanDefinitionException getType类getType(String name)抛出NoSuchBeanDefinitionException

Determine the type of the bean with the given name. 确定具有给定名称的bean的类型。 More specifically, determine the type of object that getBean(java.lang.String) would return for the given name. 更具体地说,确定给定名称的getBean(java.lang.String)返回的对象的类型。 For a FactoryBean, return the type of object that the FactoryBean creates, as exposed by FactoryBean.getObjectType(). 对于FactoryBean,返回由FactoryBean.getObjectType()公开的FactoryBean创建的对象的类型。

Translates aliases back to the corresponding canonical bean name. 将别名转换回相应的规范bean名称。 Will ask the parent factory if the bean cannot be found in this factory instance. 将询问父工厂是否在该工厂实例中找不到该bean。

Parameters: 参数:

name - the name of the bean to query name-要查询的bean的名称

Returns: 返回值:

the type of the bean, or null if not determinable bean的类型;如果不确定,则为null

Throws: 抛出:

NoSuchBeanDefinitionException - if there is no bean with the given name NoSuchBeanDefinitionException-如果不存在具有给定名称的bean

Since: 以来:

1.1.2 1.1.2

See Also: 也可以看看:

getBean(java.lang.String), isTypeMatch(java.lang.String, org.springframework.core.ResolvableType) getBean(java.lang.String),isTypeMatch(java.lang.String,org.springframework.core.ResolvableType)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM