简体   繁体   中英

Get class object from spring bean name

I'm having trouble finding the Class of a spring bean. The only information I currently have is the bean id.

Given in my application context I have the following bean:

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

I currently only have the name of the class "someConfig". How do I obtain the Class from the bean name? Is there any way of doing this without using 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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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