java - NoSuchBeanDefinitionException: No qualifying bean of type [Repository] found for dependency: expected at least 1 bean which qualifies as autowire -


i found similar issues explained many web portals. guess unique situation. getting error in spring mvc app.

org.springframework.beans.factory.unsatisfieddependencyexception: error creating bean name 'testcontroller' defined in file [c:\program files (x86)\sts-bundle\pivotal-tc-server-developer-3.1.2.release\base-instance\wtpwebapps\expt1\web-inf\classes\com\expt\controller\testcontroller.class]: unsatisfied dependency expressed through constructor argument index 0 of type [com.expt.repositories.categoryrepository]: no qualifying bean of type [com.expt.repositories.categoryrepository] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {}; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [com.expt.repositories.categoryrepository] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {}     org.springframework.beans.factory.support.constructorresolver.createargumentarray(constructorresolver.java:749)     org.springframework.beans.factory.support.constructorresolver.autowireconstructor(constructorresolver.java:185)  .....  org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [com.expt.repositories.categoryrepository] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {}     org.springframework.beans.factory.support.defaultlistablebeanfactory.raisenosuchbeandefinitionexception(defaultlistablebeanfactory.java:1373) 

the code given below. think enough code has been copied. please me understand missing , how can fix issue. great...!! code: appinitializer:

package com.expt.config;  import org.springframework.web.servlet.support.abstractannotationconfigdispatcherservletinitializer;   public class appinitialzer extends abstractannotationconfigdispatcherservletinitializer {      @override     protected class<?>[] getrootconfigclasses() {         // todo auto-generated method stub         return new class[]{springappcontinit.class, sqldevdatasource.class};     }      @override     protected class<?>[] getservletconfigclasses() {         // todo auto-generated method stub         return new class[]{webmvcconfig.class};     }      @override     protected string[] getservletmappings() {         // todo auto-generated method stub         return new string[]{"/"};     }  } 

sqldevconfig:

package com.expt.config;  import javax.sql.datasource;  import org.apache.commons.dbcp.basicdatasource; import org.springframework.context.annotation.configuration; import org.springframework.context.annotation.profile; import org.springframework.data.jpa.repository.config.enablejparepositories;  @configuration @profile("sqldev") @enablejparepositories(basepackages={"com.expt.repositories"}) public class sqldevdatasource extends abstractjpaconfig {      @override     public datasource getdatasource() {         // todo auto-generated method stub         return createbasicdatasource("jdbc:jtds:sqlserver://local:1433/mydb;", "net.sourceforge.jtds.jdbc.driver", "unma", "pwd123");     }     public basicdatasource createbasicdatasource(string jdbcurl, string driverclass, string username, string password) {         basicdatasource datasource = new basicdatasource();         datasource.seturl(jdbcurl);         datasource.setdriverclassname(driverclass);         datasource.setusername(username);         datasource.setpassword(password);         return datasource;     }  } 

springappcontinit:

package com.expt.config;  import org.springframework.context.applicationcontextinitializer; import org.springframework.core.env.configurableenvironment; import org.springframework.web.context.support.annotationconfigwebapplicationcontext;  public class springappcontinit implements applicationcontextinitializer<annotationconfigwebapplicationcontext> {      @override     public void initialize(annotationconfigwebapplicationcontext applicationcontext) {         // todo auto-generated method stub         configurableenvironment configenv = applicationcontext.getenvironment();         configenv.addactiveprofile("sqldev");     }  } 

abstrackjpaconfig:

   package com.expt.config;  import java.util.hashmap; import java.util.map;  import javax.persistence.entitymanager; import javax.persistence.entitymanagerfactory; import javax.persistence.spi.persistenceprovider; import javax.sql.datasource;  import org.hibernate.dialect.sqlserver2008dialect; import org.hibernate.jpa.hibernatepersistenceprovider; import org.springframework.context.annotation.bean; import org.springframework.orm.jpa.jpadialect; import org.springframework.orm.jpa.jpatransactionmanager; import org.springframework.orm.jpa.jpavendoradapter; import org.springframework.orm.jpa.localcontainerentitymanagerfactorybean; import org.springframework.orm.jpa.vendor.hibernatejpavendoradapter; import org.springframework.transaction.platformtransactionmanager;  import com.expt.domain.category;  public abstract class abstractjpaconfig {      public abstract datasource getdatasource();      @bean(name="entitymanagerfactory")     public localcontainerentitymanagerfactorybean entitymanagerfactory(datasource datasource){         map<string, string> properties = new hashmap<string, string>();         properties.put(org.hibernate.cfg.environment.hbm2ddl_auto, "validate");         properties.put(org.hibernate.cfg.environment.dialect, sqlserver2008dialect.class.getname());         properties.put(org.hibernate.cfg.environment.show_sql, "true");          localcontainerentitymanagerfactorybean em = new localcontainerentitymanagerfactorybean();         em.setdatasource(datasource);         //em.setpackagestoscan(expense.class.getpackage().getname());         em.setpackagestoscan(category.class.getpackage().getname());         //em.setpersistenceprovider(new hibernatepersistenceprovider());         em.setjpavendoradapter(new hibernatejpavendoradapter());         em.setjpapropertymap(properties);                return em;     }      @bean(name = "transactionmanager")     public platformtransactionmanager transactionmanager(entitymanagerfactory entitymanagerfactory) {         return new jpatransactionmanager(entitymanagerfactory);     } } 

controller:

package com.expt.controller;  import org.springframework.beans.factory.annotation.autowired; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller;  import com.expt.domain.category; import com.expt.repositories.categoryrepository;  @restcontroller @requestmapping("/test") public class testcontroller {      categoryrepository catrepo;      @autowired     public testcontroller(categoryrepository catrepo) {         this.catrepo = catrepo;     }      /*private categoryservice catsvc;      @autowired     public testcontroller(categoryservice catsvc) {         this.catsvc = catsvc;     }*/      @requestmapping("/simple")     public string test(){         return "testing";     }      @requestmapping("/json")     public iterable<category> testj(){         return catrepo.findall();     } } 

categoryrepository:

package com.expt.repositories;  import java.math.bigdecimal;  import org.springframework.data.jpa.repository.jparepository; import org.springframework.data.repository.crudrepository; import org.springframework.stereotype.repository;  import com.expt.domain.category;  @repository public interface categoryrepository extends jparepository<category, bigdecimal> {  } 

=========================================================

edit:(as there lot of comments added this, thought add these details well)

i referring https://github.com/scottfrederick/spring-music. uses public class appinitializer implements webapplicationinitializer appinitializer.

full code here, using in old project. manually registering configuration classes , working. not require when extend abstractannotationconfigdispatcherservletinitializer.

   public class appinitializer implements webapplicationinitializer{      @override     public void onstartup(servletcontext servletcontext) throws servletexception {         // todo auto-generated method stub         configureappcontextinitializers(servletcontext, springappcontinit.class.getname());          annotationconfigwebapplicationcontext rootcontext = new annotationconfigwebapplicationcontext();         rootcontext.register(repositoryconfig.class);         servletcontext.addlistener(new contextloaderlistener(rootcontext));          annotationconfigwebapplicationcontext webcontext = new annotationconfigwebapplicationcontext();         webcontext.register(webmvcconfig.class);          dispatcherservlet dispatcherservlet = new dispatcherservlet(webcontext);         servletregistration.dynamic dispatcher = servletcontext.addservlet("dispatcher", dispatcherservlet);         dispatcher.setloadonstartup(1);         dispatcher.addmapping("/");     }      private void configureappcontextinitializers(servletcontext container, string... initclassnames) {         string initializerclasses = container.getinitparameter(contextloader.context_initializer_classes_param);          string delimitedclassnames = stringutils.arraytodelimitedstring(initclassnames, " ");          if (stringutils.hastext(initializerclasses)) {             initializerclasses += " " + delimitedclassnames;         }         else {             initializerclasses = delimitedclassnames;         }          container.setinitparameter(contextloader.context_initializer_classes_param, initializerclasses);     } } 

in case of abstractannotationconfigdispatcherservletinitializer, methods getrootconfigclasses , getservletconfigclasses registering implicitly.now looking reason why above code not working. have workaround can make working reverting xmls or webapplicationinitializer. looking solution , reason if possible updated code rather api document. need tutorial. api document wont me resolve issue.

your own implementation of webapplicationinitializer , implementation extending abstractannotationconfigdispatcherservletinitializer different. behave different , because aren't registering custom applicationcontextinitializer. how register applicationcontextinitializer explained in javadoc (notice 2 methods set collection of applicationcontextinitializers.).

public class appinitialzer extends abstractannotationconfigdispatcherservletinitializer {      ...          protected applicationcontextinitializer<?>[] getrootapplicationcontextinitializers() {         return new applicationcontextinitializer[] { new springappcontinit() } ;     } } 

however applicationcontextinitializer doesn't add cannot done setting environment or system property.

spring.profiles.active=sqldev 

when don't need init.

i believe @enablejparepositories on wrong class, fact want enable shouldn't depend on profile.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -