org.springframework.beans.factory.BeanCreationException:注入自动连接的依赖项失败;

时间:2018-05-22 15:41:42

标签: java mysql spring-mvc datasource jdbctemplate

在服务器启动期间获得以下错误。

  

org.springframework.beans.factory.BeanCreationException:错误   创建名为'kuttyJavaController'的bean:注入自动装配   依赖失败;嵌套异常是   org.springframework.beans.factory.BeanCreationException:不能   autowire字段:private org.springframework.jdbc.core.JdbcTemplate   com.service.KuttyJavaController.jdbcoriensb2c;嵌套异常是   org.springframework.beans.factory.NoSuchBeanDefinitionException:没有   类型为[org.springframework.jdbc.core.JdbcTemplate]的限定bean   找到依赖:预计至少有1个bean符合条件   autowire候选人这种依赖。依赖注释:   {@ org.springframework.beans.factory.annotation.Autowired(所需=真)}

Bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd ">

    <bean id="oriensb2c"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="username" value="aa" />
            <property name="url" value="jdbc:mysql://localhost:3306/aa" />
            <property name="password" value="aa" />
    </bean>

    <bean id="oriensb2b"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="username" value="bb" />
            <property name="url" value="jdbc:mysql://localhost:3306/bb" />
            <property name="password" value="bb" />
    </bean>

    <bean id="nstoreb2c"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="username" value="cc" />
            <property name="url" value="jdbc:mysql://localhost:3306/cc" />
            <property name="password" value="cc" />
    </bean>

    <bean id="nstoreb2b"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="username" value="dd" />
            <property name="url" value="jdbc:mysql://localhost:3306/dd" />
            <property name="password" value="dd" />
    </bean>



    <!-- Definition for JDBCTemplate bean -->
    <bean id="jdbcoriensb2c" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="oriensb2c"></property>
    </bean>
    <bean id="jdbcoriensb2b" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="oriensb2b"></property>
    </bean>

    <bean id="jdbcnstoreb2c" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="nstoreb2c"></property>
    </bean>
    <bean id="jdbcnstoreb2b" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="nstoreb2b"></property>
    </bean>

    <bean id="kuttyJavaController" class="com.service.KuttyJavaController">
    <property name="jdbcoriensb2c" ref="jdbcoriensb2c"></property>
    <property name="jdbcoriensb2b" ref="jdbcoriensb2b"></property>
    <property name="jdbcnstoreb2c" ref="jdbcnstoreb2c"></property>
    <property name="jdbcnstoreb2b" ref="jdbcnstoreb2b"></property>
    </bean>
</beans>

控制器:

@Controller
public class KuttyJavaController {


    //Oriens B2c
    private JdbcTemplate jdbcoriensb2c;

    //Oriens B2b
    private JdbcTemplate jdbcoriensb2b;

    //Nstore B2c
    private JdbcTemplate jdbcnstoreb2c;

    //Nstore B2b
    private JdbcTemplate jdbcnstoreb2b;



    public void setJdbcoriensb2c(JdbcTemplate jdbcoriensb2c) {
        this.jdbcoriensb2c = jdbcoriensb2c;
    }

    public void setJdbcoriensb2b(JdbcTemplate jdbcoriensb2b) {
        this.jdbcoriensb2b = jdbcoriensb2b;
    }

    public void setJdbcnstoreb2c(JdbcTemplate jdbcnstoreb2c) {
        this.jdbcnstoreb2c = jdbcnstoreb2c;
    }

    public void setJdbcnstoreb2b(JdbcTemplate jdbcnstoreb2b) {
        this.jdbcnstoreb2b = jdbcnstoreb2b;
    }

1 个答案:

答案 0 :(得分:1)

您的bean的名称不匹配。例如,您定义了以下内容:

<bean id="nstoreb2btemplate" name="nstoreb2btemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="nstoreb2b"></property>
</bean>

然而,你用它来称呼它:

//Nstore B2b
private JdbcTemplate jdbcnstoreb2b;

这应该是(当然也是在驼峰的情况下):

//Nstore B2b
private JdbcTemplate nstoreb2btemplate;