Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 235
M06H Avatar asked Jun 19 '14 16:06

M06H


People also ask

Can not create bean Spring?

There could be numerous reasons why Spring could not able to create a bean with name X, but clue always lies on the detailed stack trace. This error always has some underlying cause e.g. a ClassNotFoundException or a NoClassDefFoundError, which potentially signal a missing JAR file in the classpath.

Does the id of a bean have to be unique throughout the XML File should be unique need not be unique should never be unique rarely unique?

The bean identifiers (id and name) Every bean has one or more ids (also called identifiers, or names; these terms refer to the same thing). These ids must be unique within the BeanFactory or ApplicationContext the bean is hosted in.

Why do we get bean creation exception?

By far, the most common cause of the BeanCreationException is Spring trying to inject a bean that doesn't exist in the context. To diagnose this type of issue, we'll first make sure the bean is declared: either in an XML configuration file using the <bean /> element.

What is BeanFactory in Spring?

Beans are java objects that are configured at run-time by Spring IoC Container. BeanFactory represents a basic IoC container which is a parent interface of ApplicationContext. BeanFactory uses Beans and their dependencies metadata to create and configure them at run-time.


1 Answers

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

like image 93
jmj Avatar answered Oct 30 '22 22:10

jmj