java - @AUTOWIRED : BeanCreationException -
i'm trying follow book title spring mvc beginner's guide , i'm stuck @ creating repository object. keep on getting beancreationexception. not sure else missed. i'm wondering if can me figure out issue.
please find below code. thanks.
beancreationexception
org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [com.packt.webstore.domain.repository.productrepository] found dependency: expected @ least 1 bean qualifies autowire candidate 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 4 inch 640 x 1136 display , 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) 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 lightest 7 inch tablet 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; } }
it seems using spring 4, not using spring boot, new spring, take advice , instead of using "pure" spring, use spring boot, awesome project ease lot working spring. spring boot lot of configuration free, easy dependency management many spring libraries , thirdy party components, won't need use xml configuration, , lot more. recommended way start spring project.
that first part of answer. second part, getting exception prolly because of type @reimeus mention. repository annotation not in com.packt.webstore marked base package component scan, spring container cannot find repository annotation.
Comments
Post a Comment