我在这里是新手,我正在尝试使用Spring创建一个电子商店,但与数据库的连接存在问题。我搜索发现其他用户也问过同样的问题,但我找不到问题。我缺乏想法,这是怎么了。
堆栈跟踪:
org.springframework.beans.factory.BeanCreationException:创建名称为'homeController'的bean时出错:自动连接依赖项的注入失败;嵌套的异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有dao.ProductDao controllers.HomeController.productDao; 嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有找到类型为[dao.ProductDao]的合格Bean作为依赖项:至少应有1个有资格作为此依赖项的自动装配候选的bean。依赖项注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
源代码:
控制者
package controllers;
import java.io.IOException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import dao.ProductDao;
import model.Product;
@Controller
public class HomeController {
@Autowired
private ProductDao productDao;
@RequestMapping("/")
public String home(){
return "home";
}
@RequestMapping("/productList")
public String getProducts(Model model) {
List<Product> products = productDao.getAllProducts();
model.addAttribute("products", products);
return "productList";
}
@RequestMapping("/productList/viewProduct/{productId}")
public String viewProduct(@PathVariable String productId, Model model) throws IOException{
Product product = productDao.getProductById(productId);
model.addAttribute(product);
return "viewProduct";
}
Run Code Online (Sandbox Code Playgroud)
} …