[我正在尝试Spring MVC,当我尝试在控制器类中添加@Autowired时,出现以下错误:

问题描述 投票:0回答:2

<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";
	}
}
java spring-mvc spring-data-jpa spring-jdbc
2个回答
0
投票

似乎您没有在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>

0
投票

您需要在XML文件中定义DataSource bean,然后只有使用才能使用@Autowired注释来配置该bean


0
投票

由于您具有存储库实现,请尝试添加@Qualifier来区分存储库,例如:

@Qualifier("dresses")
@Repository
public class DressRepository implements InventoryRepository {
}

@Qualifier("shoes")
@Repository
public class ShoeRepository implements InventoryRepository {
}

public PurchaseDeptService(@Qualifier("dresses") InventoryRepository repository) {
    this.repository = repository;
}
© www.soinside.com 2019 - 2024. All rights reserved.