【发布时间】:2017-02-15 18:28:14
【问题描述】:
我有一个基于 xml-config 的 spring 应用程序,其中我的一个 bean 需要一个 com.typesafe.config.Config 参数作为构造函数参数。 为此,我们有一个@Configuration 类,其中一个方法(注释为@Bean)返回一个com.typesafe.config.Config 对象。
但是当spring启动时它会抱怨“无法实例化[com.typesafe.config.Config]:指定的类是一个接口”
如何通过 xml 将 getconfig 返回对象注入到构造函数中?
这里是为了摆脱应用逻辑而简化的sn-p代码:
import com.typesafe.config.Config;
...
public class myFilter implements Filter {
public myFilter(Config aconfig) {
...
}
}
然后我有一个如下的 JavaConfig 类,它创建了我需要在 myFilter 构造函数中注入的 bean:
@org.springframework.context.annotation.Configuration
public class TSConfiguration {
...
@Bean(name = "app-config")
public Config getConfig() {
...
}
}
我在 spring.xml 文件中添加了以下内容:
<beans:bean class="TSConfiguration" />
<beans:bean id="app-config" class="com.typesafe.config.Config"/>
<beans:bean id="theFilter" class="myFilter">
<beans:constructor-arg index="0" ref="app-config"/>
</beans:bean>
这是 Spring 的例外情况:
15-Feb-2017 13:19:55.463 SEVERE [localhost-startStop-1] org.springframework.web.context.ContextLoader.initWebApplicationContext Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'app-config' defined in ServletContext resource [/WEB-INF/spring-security.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.typesafe.config.Config]: Specified class is an interface
谢谢。
【问题讨论】:
标签: spring dependency-injection spring-java-config spring-bean typesafe-config