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

2

5
<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)

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 ?

Quach answered 19/6, 2014 at 16:36 Comment(0)
W
9

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

Waterloo answered 19/6, 2014 at 16:39 Comment(0)
H
1

I got the same error and in my bean class, I declared the default constructor which was not declared earlier and it solved my problem.

So My complete error is like Error creating bean with name 'student1' defined in class path resource [config.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.springcore.springcore.Student]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.springcore.springcore.Student.()

Hartsfield answered 9/10, 2022 at 14:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.