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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:27:28  来源:igfitidea点击:

Error creating bean with name defined in class path resource [application-context.xml] in spring framework

javaxmlspringspring-mvc

提问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 Stringvalue you want to inject referred bean

因为你不想注入String你想要注入引用 bean 的值