【问题标题】:I am trying Spring MVC and when I add @Autowired in my controller class I get following error:我正在尝试 Spring MVC,当我在控制器类中添加 @Autowired 时,出现以下错误:
【发布时间】:2020-06-07 22:00:48
【问题描述】:
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:c="http://www.springframework.org/schema/c"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

    <context:component-scan base-package="com.packt.webstore.domain" />
    <context:annotation-config></context:annotation-config>

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

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>

org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为“productController”的 bean 时出错:不满意 通过字段“productRepository”表达的依赖关系;嵌套的 例外是 org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为“productRepositoryImpl”的 bean 时出错:不满意 通过方法 'setDataSource' 参数 0 表示的依赖关系; 嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 “javax.sql.DataSource”类型的合格 bean 可用:预计在 至少 1 个符合自动装配候选资格的 bean。依赖 注释:{}

package com.packt.webstore.domain.repository.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.packt.webstore.domain.Product;
import com.packt.webstore.domain.repository.ProductRepository;

@Repository
public class ProductRepositoryImpl implements ProductRepository {

    private NamedParameterJdbcTemplate jdbcTemplate;

    @Autowired
    private void setDataSource(DataSource dataSource) {
        this.jdbcTemplate=new NamedParameterJdbcTemplate(dataSource);
    }

    @Override
    public List<Product> getAllProducts() {
        Map<String, Object>params=new HashMap<String,Object>();
        List<Product>result=jdbcTemplate.query("SELECT * FROM PRODUCTS", params, new ProductMapper());
        return result;
    }

    private static final class ProductMapper implements org.springframework.jdbc.core.RowMapper<Product> {
        public Product mapRow(ResultSet rs,int rownum) throws SQLException{
            Product product=new Product();
            product.setName(rs.getString("name"));
            return product;
        }
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <mvc:annotation-driven enable-matrix-variables="true"></mvc:annotation-driven>

    <context:component-scan base-package="com.packt"/>

     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
     p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
</beans>
package com.packt.webstore.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.packt.webstore.domain.repository.ProductRepository;

@Controller
public class ProductController {

    @Autowired
    private ProductRepository productRepository;


    @RequestMapping("/products")
    public String list(Model model) {
        model.addAttribute("products",productRepository.getAllProducts());
        return "products";
    }
}

【问题讨论】:

  • 错误提示,您缺少数据源...
  • 如果你是从头开始开发,请不要使用XML - 使用spring DSL来创建bean等。

标签: java spring-mvc spring-data-jpa spring-jdbc


【解决方案1】:

您似乎没有在 xml 文件中配置 data-source

尝试在xml文件中添加以下配置

<bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/yourdbname" />
        <property name="username" value="root" />
        <property name="password" value="password" />
    </bean>

【讨论】:

  • 我刚刚添加了 jdbc-confif 文件查看。
  • 为什么有两个xml文件尝试只有一个xml文件
  • 是的,当我在 spring-servlet xml 文件中添加数据源时它起作用了。但以前它适用于 2 个 XML 文件。你能解释一下为什么会发生吗?
  • 提前谢谢?
  • 没有看到代码很难知道你是如何配置你的 xml 文件的,但是如果你愿意的话,可以按照 - mkyong.com/spring/load-multiple-spring-bean-configuration-file this
【解决方案2】:

您需要在您的 XML 文件中定义 DataSource bean,然后只能使用可以使用 @Autowired 注释来配置该 bean,或者您可以通过以下方式使用 @Autowired 注释:

@自动连线 私有 NamedParameterJdbcTemplate jdbcTemplate;

【讨论】:

  • 我只是将jdbc配置分离到另一个xml文件中,以便于使用。
猜你喜欢
  • 1970-01-01
  • 2011-11-21
  • 1970-01-01
  • 2022-06-14
  • 1970-01-01
  • 2016-09-11
  • 1970-01-01
  • 2023-02-04
  • 2020-07-29
相关资源
最近更新 更多