简体   繁体   中英

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM