java - @Autowired can't find repository beans -
i'am bit confused spring data jpa repositories used in "annotation only" context. when starting tomcat server on deploying application, following error:
grave: containerbase.addchild: start: org.apache.catalina.lifecycleexception: failed start component [standardengine[catalina].standardhost[localhost].standardcontext[/dashboard]] caused by: org.springframework.beans.factory.beancreationexception: error creating bean name 'archivingservice': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: private com.mycompany.dashboard.persistence.repository.requestsrepository com.mycompany.dashboard.core.services.archiving.archivingserviceimpljpa.requestsrepository; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [com.mycompany.dashboard.persistence.repository.requestsrepository] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)} caused by: org.springframework.beans.factory.beancreationexception: not autowire field: private com.mycompany.dashboard.persistence.repository.requestsrepository com.mycompany.dashboard.core.services.archiving.archivingserviceimpljpa.requestsrepository; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [com.mycompany.dashboard.persistence.repository.requestsrepository] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)} caused by: org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [com.mycompany.dashboard.persistence.repository.requestsrepository] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)}
the error clear can't figure out wrong. first suspicion rootcontext created before persistencecontext, , repository beans not known when autowiring them in service class. second suspicion missing concerning use of annotations.
my configuration classes:
package com.mycompany.config; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.componentscan; import org.springframework.context.annotation.configuration; import com.mycompany.core.services.archiving.archivingservice; import com.mycompany.core.services.archiving.archivingserviceimpljpa; import com.mycompany.core.services.restore.restoreservice; import com.mycompany.core.services.restore.restoreserviceimplapi; import com.mycompany.core.util.statusconverter; /** * configuration class used define core(service) beans * * */ @configuration @componentscan(basepackages = { "com.mycompany.core.services", "com.mycompany.core.util"}) public class coreconfig { @bean public archivingservice archivingservice() { return new archivingserviceimpljpa(); } }
mvcconfig:
package com.mycompany.dashboard.config; import java.util.arraylist; import java.util.list; import org.dozer.dozerbeanmapper; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.componentscan; import org.springframework.context.annotation.configuration; import org.springframework.http.converter.httpmessageconverter; import org.springframework.http.converter.json.mappingjackson2httpmessageconverter; import org.springframework.web.servlet.config.annotation.enablewebmvc; import org.springframework.web.servlet.config.annotation.webmvcconfigurer; import org.springframework.web.servlet.config.annotation.webmvcconfigureradapter; import com.mycompany.dashboard.core.util.apiobjectmapper; /** * configuration class used define web beans, these beans used * rest service * * */ @configuration @enablewebmvc @componentscan(basepackages = { "com.mycompany.dashboard.rest.controller", "com.mycompany.dashboard.rest.filter" }) public class mvcconfig extends webmvcconfigureradapter implements webmvcconfigurer { private static final string mapping_file = "meta-inf/dozer/global-dto.xml"; /** * configures dozer mapping , objectmapper * * @return dozerbeanmapper */ @bean public dozerbeanmapper dozermapper() { final dozerbeanmapper mapper = new dozerbeanmapper(); final list<string> mappingfiles = new arraylist<>(); mappingfiles.add(mapping_file); mapper.setmappingfiles(mappingfiles); return mapper; } @bean public apiobjectmapper apiobjectmapper() { return new apiobjectmapper(); } /** * configure jackson objects json converter */ @override public void configuremessageconverters( list<httpmessageconverter<?>> converters) { final mappingjackson2httpmessageconverter converter = new mappingjackson2httpmessageconverter(); converter.setobjectmapper(apiobjectmapper()); converters.add(converter); } }
persistencejpaconfig:
package com.mycompany.dashboard.config; import java.util.properties; import javax.persistence.entitymanagerfactory; import javax.sql.datasource; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.dao.annotation.persistenceexceptiontranslationpostprocessor; import org.springframework.data.jpa.repository.config.enablejparepositories; import org.springframework.jdbc.datasource.drivermanagerdatasource; 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 org.springframework.transaction.annotation.enabletransactionmanagement; /** * configuration class used define persistence beans * */ @configuration @enabletransactionmanagement @enablejparepositories("com.mycompany.dashboard.persistence.repository") public class persistencejpaconfig { /** * gets localcontainerentitymanagerfactorybean , configures * * @return localcontainerentitymanagerfactorybean */ @bean public localcontainerentitymanagerfactorybean entitymanagerfactory() { localcontainerentitymanagerfactorybean em = new localcontainerentitymanagerfactorybean(); em.setdatasource(datasource()); em.setpackagestoscan(new string[] { "com.mycompany.persistence.model" }); jpavendoradapter vendoradapter = new hibernatejpavendoradapter(); em.setjpavendoradapter(vendoradapter); em.setjpaproperties(additionalproperties()); return em; } /** * configures connection database * * @return datasource */ @bean public datasource datasource() { drivermanagerdatasource datasource = new drivermanagerdatasource(); datasource.setdriverclassname("com.mysql.jdbc.driver"); datasource.seturl("jdbc:oracle:thin:@dbdev:1521:dev"); datasource.setusername("contintegr"); datasource.setpassword("8944ce07176988d6f30cc021471ebc9a"); return datasource; } /** * sets platformtransactionmanager entitymanager * * @param entitymanagerfactory * @return platformtransactionmanager */ @bean public platformtransactionmanager transactionmanager( entitymanagerfactory emf) { jpatransactionmanager transactionmanager = new jpatransactionmanager(); transactionmanager.setentitymanagerfactory(emf); return transactionmanager; } @bean public persistenceexceptiontranslationpostprocessor exceptiontranslation() { return new persistenceexceptiontranslationpostprocessor(); } properties additionalproperties() { properties properties = new properties(); // properties.setproperty("hibernate.hbm2ddl.auto", "create-drop"); // properties.setproperty("hibernate.dialect", // "org.hibernate.dialect.mysql5dialect"); return properties; } }
webappinitializer:
package com.mycompany.dashboard.config; import java.util.set; import javax.servlet.servletcontext; import javax.servlet.servletexception; import javax.servlet.servletregistration; import org.springframework.web.webapplicationinitializer; import org.springframework.web.context.contextloaderlistener; import org.springframework.web.context.webapplicationcontext; import org.springframework.web.context.support.annotationconfigwebapplicationcontext; import org.springframework.web.servlet.dispatcherservlet; import com.mycompany.dashboard.rest.filter.corsfilter; /** * configuration class centralizes core, web , persistence * configurations * */ public class webappinitializer implements webapplicationinitializer { // api servlet name private static final string api_servlet_name = "api"; // api servlet mapping private static final string api_servlet_mapping = "/api/*"; public void onstartup(servletcontext servletcontext) throws servletexception { webapplicationcontext rootcontext = createrootcontext(servletcontext); configurepersistence(rootcontext); configurespringmvc(servletcontext, rootcontext); } private webapplicationcontext createrootcontext( servletcontext servletcontext) { annotationconfigwebapplicationcontext rootcontext = new annotationconfigwebapplicationcontext(); rootcontext.register(coreconfig.class); rootcontext.refresh(); servletcontext.addlistener(new contextloaderlistener(rootcontext)); servletcontext.addfilter("corsfilter", new corsfilter()) .addmappingforurlpatterns(null, false, "/*"); ; servletcontext.setinitparameter("defaulthtmlescape", "true"); return rootcontext; } private void configurespringmvc(servletcontext servletcontext, webapplicationcontext rootcontext) { annotationconfigwebapplicationcontext mvccontext = new annotationconfigwebapplicationcontext(); mvccontext.register(mvcconfig.class); mvccontext.setparent(rootcontext); servletregistration.dynamic appservlet = servletcontext.addservlet( api_servlet_name, new dispatcherservlet(mvccontext)); appservlet.setloadonstartup(1); set<string> mappingconflicts = appservlet .addmapping(api_servlet_mapping); } private void configurepersistence(webapplicationcontext rootcontext) { annotationconfigwebapplicationcontext persistencecontext = new annotationconfigwebapplicationcontext(); persistencecontext.register(persistencejpaconfig.class); persistencecontext.setparent(rootcontext); } }
my service class:
package com.mycompany.dashboard.core.services.archiving; import java.sql.timestamp; import java.util.linkedlist; import java.util.list; import org.springframework.beans.factory.annotation.autowired; import com.mycompany.dashboard.arcsysdashboard.core.util.statusconverter; import com.mycompany.dashboard.persistence.model.request; import com.mycompany.dashboard.persistence.repository.archivingsrepository; import com.mycompany.dashboard.persistence.repository.requestsrepository; import com.mycompany.dashboard.rest.domain.restrequest; /** * implementing class of archiving service interface using spring data jpa * repositories * */ public class archivingserviceimpljpa implements archivingservice { @autowired private requestsrepository requestsrepository; @autowired private archivingsrepository archivingsrepository; @autowired private statusconverter statusconverter; @override public list<restrequest> findlastarchivingrequests() { list<restrequest> recordrequests = new linkedlist<restrequest>(); list<restrequest> restrequests = new linkedlist<restrequest>(); list<request> requests = requestsrepository.findfivelastrequests("a"); (request request : requests) { restrequest restrequest = new restrequest(new timestamp(request .getdate().gettime()), this.statusconverter.getstatus( request.getlaststatus()).tostring(), archivingsrepository .findone(request.getlotident()).getcode()); restrequests.add(restrequest); } return recordrequests; } }
my repository classes:
package com.mycompany.dashboard.persistence.repository; import java.rmi.server.uid; import org.springframework.data.jpa.repository.jparepository; import com.mycompany.dashboard.persistence.model.record; /** * spring data repository interface used manage records * */ public interface archivingsrepository extends jparepository<record, uid> { record findone(long id); } package com.mycompany.dashboard.persistence.repository; import java.rmi.server.uid; import java.util.list; import org.springframework.data.jpa.repository.jparepository; import org.springframework.data.jpa.repository.query; import org.springframework.data.repository.norepositorybean; import org.springframework.data.repository.query.param; import org.springframework.stereotype.repository; import com.mycompany.dashboard.persistence.model.request; /** * spring data repository interface used manage requests */ public interface requestsrepository extends jparepository<request, uid> { @query("select top 5 req request req req.owner = :name , req.type = paramtype") list<request> findfivelastrequestsbyownerandtype( @param("owner") string author, @param("type") string paramtype); @query("select top 5 req request req req.type = a") list<request> findfivelastrequests(@param("type") string paramtype); }
place @import(persistencejpaconfig.class)
coreconfig
class seems configuration class isn't being picked or @ all.
Comments
Post a Comment