Java 在 spring 框架中使用在类路径资源 [application-context.xml] 中定义的名称创建 bean 时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24311866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error creating bean with name defined in class path resource [application-context.xml] in spring framework
提问by M06H
<bean id="MyDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</bean>
<bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg value="MyDataSource"/>
</bean>
Error creating bean with name 'template' defined in class path resource [application-context.xml]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
使用在类路径资源 [application-context.xml] 中定义的名称“模板”创建 bean 时出错:无法解析匹配的构造函数(提示:为简单参数指定索引/类型/名称参数以避免类型歧义)
I am not sure what I am doing wrong here to get above error? Have all of it defined in properties file correctly with correct variable name. what are the things to check for ?
我不确定我在这里做错了什么来获得上述错误?使用正确的变量名称在属性文件中正确定义所有这些。需要检查什么?
采纳答案by Jigar Joshi
change
改变
<bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg value="MyDataSource"/>
</bean>
to
到
<bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="MyDataSource"/>
</bean>
because you don't want to inject String
value you want to inject referred bean
因为你不想注入String
你想要注入引用 bean 的值