java - 从spring bean名称获取类对象

标签 java spring

我无法找到 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/

相关文章:

java - 在应用程序运行时更新 Spring Boot 属性的更好方法

java - UnsatisfiedDependencyException - 嵌套异常是 java.lang.NoClassDefFoundError : org/dom4j/io/STAXEventReader

java - 使图形对象移动

java - Java Spring 应用程序可以利用 swagger 吗?

java - 发出 HTTP 请求时在 Java 中实现超时的最佳方法

java - 我可以只使用 Spring security 的 CSRF 功能而不使用其 LOGIN 和 LOGOUT 功能吗?

java - Maven构建成功但仍然出现404错误未找到

java - Android 首选项 Null 异常

java - ArrayList 中每个对象的删除按钮

java - HazelcastInstance bean - 正确的销毁方法是什么?