M06H
M06H

Reputation: 1783

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

<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 ?

Upvotes: 5

Views: 41583

Answers (2)

sanket kushwaha
sanket kushwaha

Reputation: 9

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.()

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240900

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

Upvotes: 9

Related Questions