【问题标题】:Error creating bean with name defined in class path resource [application-context.xml] in spring framework在 Spring 框架中创建名称在类路径资源 [application-context.xml] 中定义的 bean 时出错
【发布时间】:2014-08-10 07:15:24
【问题描述】:
<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>

在类路径中定义名称为“模板”的 bean 创建错误 资源 [application-context.xml]:无法解析匹配 构造函数(提示:为简单指定索引/类型/名称参数 避免类型歧义的参数)

我不确定我在这里做错了什么才能得到上述错误?使用正确的变量名在属性文件中正确定义所有内容。有什么要检查的?

【问题讨论】:

    标签: java xml spring spring-mvc


    【解决方案1】:

    改变

    <bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg value="MyDataSource"/>
    </bean>
    

    <bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg ref="MyDataSource"/>
    </bean>
    

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

    【讨论】:

      猜你喜欢
      • 2017-12-25
      • 2016-07-15
      • 2015-09-24
      • 2019-07-02
      • 1970-01-01
      • 2015-05-10
      • 2020-03-25
      • 1970-01-01
      • 2020-03-25
      相关资源
      最近更新 更多