Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to follow a book title Spring MVC beginner's guide and I'm stuck at creating repository object. I keep on getting a BeanCreationException. Not sure what else I missed. I'm wondering if somebody can help me to figure out this issue.

Please find below my code. Thanks.

BeanCreationException

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.packt.webstore.domain.repository.ProductRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

XML FILE:

<?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"
xsi:schemaLocation="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-4.0.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

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


<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

ProductCrontroller:

package com.packt.webstore.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.packt.webstore.domain.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;

@Controller
public class ProductController  {

    @Autowired
    private ProductRepository productRepository;


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

        return "products";
    }
}

ProductRepository:

package com.packt.webstore.domain.repository;
import java.util.List;
import com.packt.webstore.domain.Product;

public interface ProductRepository {

    List<Product>getAllProducts();  

}

InMemoryProductRepository:

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

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.packt.webstore.domain.Product;
import com.packt.webstore.domain.repository.ProductRepository;

@Repository
public class InMemoryProductRepository implements ProductRepository{

    private List<Product> listOfProducts=new ArrayList<Product>();

    public InMemoryProductRepository(){
        Product iphone=new Product("P1234","iPhone 6", new BigDecimal(500));

        iphone.setDescription("Apple iphone 6 smartphone with 4 inch 640 x 1136 display and 8-megapixel rear camera");
        iphone.setCategory("Smart Phone");
        iphone.setManufacturer("Apple");
        iphone.setunitInStock(1000);

        Product laptop_dell=new Product("P1235","Dell Inspiron", new BigDecimal(700));

        laptop_dell.setDescription("Dell Inspiron 14-inch Laptop (Black) with 3rd Generation Intel Core processors");
        laptop_dell.setCategory("Laptop");
        laptop_dell.setManufacturer("Dell");
        laptop_dell.setunitInStock(1000);

        Product tablet_Nexus=new Product("P1236","Nexus 7", new BigDecimal(300));

        tablet_Nexus.setDescription("Google Nexus 7 is the lightest 7 inch tablet with a QualComm Snapdragon S4 Pro Processor");
        tablet_Nexus.setCategory("Tablet");
        tablet_Nexus.setManufacturer("Google");
        tablet_Nexus.setunitInStock(1000);

        listOfProducts.add(iphone);
        listOfProducts.add(laptop_dell);
        listOfProducts.add(tablet_Nexus);
    }

    public List<Product>getAllProducts(){
        return listOfProducts;
    }
}     
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
186 views
Welcome To Ask or Share your Answers For Others

1 Answer

It seems you are using Spring 4, but not using Spring Boot, probably you are new to Spring, so take my advice and instead of using "pure" Spring, use Spring Boot, which is an awesome project which ease a lot working with Spring. With Spring Boot you will get a lot of configuration for free, you will get easy dependency management for many Spring libraries and thirdy party components, you won't need to use XML for configuration, and a lot more. Currently is the recommended way to start a Spring project.

That was the first part of my answer. The second part, you are getting that exception prolly because of the type @Reimeus mention. As your repository annotation is not in the com.packt.webstore you marked as the base package for component scan, then the Spring container cannot find a Repository annotation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...