java - Use 'spring boot + mybatis' projects created using 'spring Application' can start, if you use an external tomcat, dao error injection -


"spring boot + mybatis" projects created , “springapplication.run” can start, if use external tomcat, dao error injection

app @configuration @componentscan(basepackages = "com.grass.module", excludefilters = {@componentscan.filter(type = filtertype.annotation, value = {controller.class})}) @enableaspectjautoproxy(proxytargetclass = true) public class appconfig {     @bean     public localvalidatorfactorybean validator() {         return new localvalidatorfactorybean();     } }  application.java  @configuration @enableautoconfiguration @componentscan public class application {     public static void main(string[] args) {         springapplication.run(application.class, args);     } }  datasourceconfig.java @configuration @enabletransactionmanagement @mapperscan("com.grass.module") public class datasourceconfig implements environmentaware {      private relaxedpropertyresolver propertyresolver;      private static logger logger = loggerfactory.getlogger(datasourceconfig.class);      @override     public void setenvironment(environment env) {         this.propertyresolver = new relaxedpropertyresolver(env, "db.");     }      @bean     public datasource datasource() {         logger.info("================================start  init  datasource =======================");         druiddatasource datasource = new druiddatasource();         datasource.seturl(propertyresolver.getproperty("jdbc.url"));         datasource.setdriverclassname(propertyresolver.getproperty("jdbc.driverclass"));         datasource.setusername(propertyresolver.getproperty("jdbc.username"));         datasource.setpassword(propertyresolver.getproperty("jdbc.password"));         logger.info("================================end   init  datasource=======================");         return datasource;     }      @bean(name = "transactionmanager")     public datasourcetransactionmanager transactionmanager(datasource datasource) {         datasourcetransactionmanager datasourcetransactionmanager = new datasourcetransactionmanager(datasource);         return datasourcetransactionmanager;      }       @bean(name = "sqlsessionfactory")     public sqlsessionfactory sqlsessionfactory(datasource datasource) throws exception {         logger.info("================================start  init  sqlsessionfactory =======================");         sqlsessionfactorybean sessionfactory = new sqlsessionfactorybean();         sessionfactory.setdatasource(datasource);         interceptor[] interceptors = new interceptor[1];         pagehelper pagehelper = new pagehelper();         properties properties = new properties(); //      <!-- 4.0.0以后版本可以不设置该参数 --> //        <property name="dialect" value="mysql"/> //        <!-- 该参数默认为false --> //        <!-- 设置为true时,会将rowbounds第一个参数offset当成pagenum页码使用 --> //        <!-- 和startpage中的pagenum效果一样--> //        <property name="offsetaspagenum" value="true"/> //        <!-- 该参数默认为false --> //        <!-- 设置为true时,使用rowbounds分页会进行count查询 --> //        <property name="rowboundswithcount" value="true"/> //        <!-- 设置为true时,如果pagesize=0或者rowbounds.limit = 0就会查询出全部的结果 --> //        <!-- (相当于没有执行分页查询,但是返回结果仍然是page类型)--> //        <property name="pagesizezero" value="true"/> //        <!-- 3.3.0版本可用 - 分页参数合理化,默认false禁用 --> //        <!-- 启用合理化时,如果pagenum<1会查询第一页,如果pagenum>pages会查询最后一页 --> //        <!-- 禁用合理化时,如果pagenum<1或pagenum>pages会返回空数据 --> //        <property name="reasonable" value="false"/> //        <!-- 3.5.0版本可用 - 为了支持startpage(object params)方法 --> //        <!-- 增加了一个`params`参数来配置参数映射,用于从map或servletrequest中取值 --> //        <!-- 可以配置pagenum,pagesize,count,pagesizezero,reasonable,orderby,不配置映射的用默认值 --> //        <!-- 不理解该含义的前提下,不要随便复制该配置 --> //        <property name="params" value="pagenum=start;pagesize=limit;"/> //        <!-- 支持通过mapper接口参数来传递分页参数 --> //        <property name="supportmethodsarguments" value="true"/> //        <!-- always总是返回pageinfo类型,check检查返回类型是否为pageinfo,none返回page --> //        <property name="returnpageinfo" value="check"/>         properties.put("dialect", "mysql");         properties.put("rowboundswithcount", true);         properties.put("pagesizezero", false);         properties.put("reasonable", true);         properties.put("supportmethodsarguments", true);         properties.put("returnpageinfo", "check");         pagehelper.setproperties(properties);         interceptors[0] = pagehelper;         sessionfactory.setplugins(interceptors);         sessionfactory.settypealiasespackage(propertyresolver.getproperty("mybatis.typealiasespackage"));         sessionfactory.setmapperlocations(new pathmatchingresourcepatternresolver().getresources(propertyresolver.getproperty("mybatis.mapperlocations")));         logger.info("================================end  init  sqlsessionfactory =======================");         return sessionfactory.getobject();     }  } mvcconfig.java  @configuration @enablewebmvc @componentscan(basepackages = {"com.grass.module"}, includefilters = {@componentscan.filter(type = filtertype.annotation, value = {controller.class}) }) @configurationproperties(prefix = "spring.mvc.") public class mvcconfig extends webmvcconfigureradapter {     @value("prefix")     private string prefix;     @value("suffix")     private string suffix;      @override     public void configuredefaultservlethandling(defaultservlethandlerconfigurer configurer) {         configurer.enable();     }      @bean     public internalresourceviewresolver viewresolver() {         internalresourceviewresolver resolver = new internalresourceviewresolver();         resolver.setprefix(prefix);         resolver.setsuffix(suffix);         return resolver;     }      @override     public void addviewcontrollers(viewcontrollerregistry registry) {         registry.addviewcontroller("/").setviewname("forward:/index.html");         registry.setorder(ordered.highest_precedence);         super.addviewcontrollers(registry);     }      @override     public void addresourcehandlers(resourcehandlerregistry registry) {         registry.addresourcehandler("/static/**")                 .addresourcelocations("classpath:/static/");     }  }  webinitializer.java      public class webinitializer extends abstractannotationconfigdispatcherservletinitializer {     /*      * dispatcherservlet的映射路径      */     @override     protected string[] getservletmappings() {         return new string[]{"/"};     }      /*       * 应用上下文,除web部分       */     @suppresswarnings({"unchecked", "rawtypes"})     @override     protected class[] getrootconfigclasses() {         return new class[]{appconfig.class};     }      /*       * web上下文       */     @suppresswarnings({"unchecked", "rawtypes"})     @override     protected class[] getservletconfigclasses() {         return new class[]{mvcconfig.class};     }      /*       * 注册过滤器,映射路径与dispatcherservlet一致,路径不一致的过滤器需要注册到另外的webapplicationinitializer中       */     @override     protected filter[] getservletfilters() {         characterencodingfilter characterencodingfilter = new characterencodingfilter();         characterencodingfilter.setencoding("utf-8");         characterencodingfilter.setforceencoding(true);         return new filter[]{characterencodingfilter};     } } usercontroller.java      @controller @requestmapping("/user") public class usercontroller {      @autowired     private userservice userservice;      @requestmapping("/findall")     @responsebody     public list<userentity> findall() {         return userservice.findall();     } }  userservice.java      @service public class userservice {     @autowired     private userdao userdao;      public list<userentity> findall() { //        return null;         return userdao.findall();     } }  userdao.java      @repository public interface userdao {     @select("select name,age sys_user")     list<userentity> findall(); } 

tomcat run

org.springframework.beans.factory.support.defaultsingletonbeanregistry 221 getsingleton - creating shared instance of singleton bean 'userservice' 16:26:37.201 debug org.springframework.beans.factory.support.abstractautowirecapablebeanfactory 448 createbean - creating instance of bean 'userservice' 16:26:37.206 debug org.springframework.beans.factory.annotation.injectionmetadata 72 checkconfigmembers - registered injected element on class [com.grass.module.sys.service.userservice]: autowiredfieldelement private com.grass.module.sys.dao.userdao com.grass.module.sys.service.userservice.userdao 16:26:37.206 debug org.springframework.beans.factory.support.abstractautowirecapablebeanfactory 529 docreatebean - eagerly caching bean 'userservice' allow resolving potential circular references 16:26:37.210 debug org.springframework.beans.factory.annotation.injectionmetadata 86 inject - processing injected element of bean 'userservice': autowiredfieldelement private com.grass.module.sys.dao.userdao com.grass.module.sys.service.userservice.userdao 16:26:37.215 warn  org.springframework.context.support.abstractapplicationcontext 546 refresh - exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.beancreationexception: error creating bean name 'userservice': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: private com.grass.module.sys.dao.userdao com.grass.module.sys.service.userservice.userdao; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [com.grass.module.sys.dao.userdao] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)} 16:26:37.215 debug org.springframework.beans.factory.support.defaultsingletonbeanregistry 512 destroysingletons - destroying singletons in org.springframework.beans.factory.support.defaultlistablebeanfactory@6ba90c6e: defining beans [org.springframework.context.annotation.internalconfigurationannotationprocessor,org.springframework.context.annotation.internalautowiredannotationprocessor,org.springframework.context.annotation.internalrequiredannotationprocessor,org.springframework.context.annotation.internalcommonannotationprocessor,org.springframework.context.event.internaleventlistenerprocessor,org.springframework.context.event.internaleventlistenerfactory,appconfig,org.springframework.context.annotation.configurationclasspostprocessor.importawareprocessor,org.springframework.context.annotation.configurationclasspostprocessor.enhancedconfigurationprocessor,userservice,validator,org.springframework.aop.config.internalautoproxycreator]; root of factory hierarchy 16:26:37.218 error org.springframework.web.context.contextloader 351 initwebapplicationcontext - context initialization failed org.springframework.beans.factory.beancreationexception: error creating bean name 'userservice': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: private com.grass.module.sys.dao.userdao com.grass.module.sys.service.userservice.userdao; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [com.grass.module.sys.dao.userdao] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)}     @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeanpostprocessor.java:334) ~[spring-beans-4.2.5.release.jar:4.2.5.release]     @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.populatebean(abstractautowirecapablebeanfactory.java:1214) ~[spring-beans-4.2.5.release.jar:4.2.5.release]     @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:543) ~[spring-beans-4.2.5.release.jar:4.2.5.release]     @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:482) ~[spring-beans-4.2.5.release.jar:4.2.5.release]     @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:306) ~[spring-beans-4.2.5.release.jar:4.2.5.release]     @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:230) ~[spring-beans-4.2.5.release.jar:4.2.5.release]     @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:302) ~[spring-beans-4.2.5.release.jar:4.2.5.release]     @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:197) ~[spring-beans-4.2.5.release.jar:4.2.5.release]     @ org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:772) ~[spring-beans-4.2.5.release.jar:4.2.5.release]     @ org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:839) ~[spring-context-4.2.5.release.jar:4.2.5.release]     @ org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:538) ~[spring-context-4.2.5.release.jar:4.2.5.release]     @ org.springframework.web.context.contextloader.configureandrefreshwebapplicationcontext(contextloader.java:444) ~[spring-web-4.2.5.release.jar:4.2.5.release]     @ org.springframework.web.context.contextloader.initwebapplicationcontext(contextloader.java:326) ~[spring-web-4.2.5.release.jar:4.2.5.release]     @ org.springframework.web.context.contextloaderlistener.contextinitialized(contextloaderlistener.java:107) ~[spring-web-4.2.5.release.jar:4.2.5.release]     @ org.apache.catalina.core.standardcontext.listenerstart(standardcontext.java:4810) ~[catalina.jar:8.0.32]     @ org.apache.catalina.core.standardcontext.startinternal(standardcontext.java:5255) ~[catalina.jar:8.0.32]     @ org.apache.catalina.util.lifecyclebase.start(lifecyclebase.java:147) ~[catalina.jar:8.0.32]     @ org.apache.catalina.core.containerbase.addchildinternal(containerbase.java:725) ~[catalina.jar:8.0.32]     @ org.apache.catalina.core.containerbase.addchild(containerbase.java:701) ~[catalina.jar:8.0.32]     @ org.apache.catalina.core.standardhost.addchild(standardhost.java:717) ~[catalina.jar:8.0.32]     @ org.apache.catalina.startup.hostconfig.manageapp(hostconfig.java:1692) ~[catalina.jar:8.0.32]     @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) ~[?:1.8.0_73]     @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) ~[?:1.8.0_73]     @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) ~[?:1.8.0_73]     @ java.lang.reflect.method.invoke(method.java:497) ~[?:1.8.0_73]     @ org.apache.tomcat.util.modeler.basemodelmbean.invoke(basemodelmbean.java:300) ~[tomcat-coyote.jar:8.0.32]     @ com.sun.jmx.interceptor.defaultmbeanserverinterceptor.invoke(defaultmbeanserverinterceptor.java:819) ~[?:1.8.0_73]     @ com.sun.jmx.mbeanserver.jmxmbeanserver.invoke(jmxmbeanserver.java:801) ~[?:1.8.0_73]     @ org.apache.catalina.mbeans.mbeanfactory.createstandardcontext(mbeanfactory.java:465) ~[catalina.jar:8.0.32]     @ org.apache.catalina.mbeans.mbeanfactory.createstandardcontext(mbeanfactory.java:415) ~[catalina.jar:8.0.32]     @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) ~[?:1.8.0_73]     @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) ~[?:1.8.0_73]     @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) ~[?:1.8.0_73]     @ java.lang.reflect.method.invoke(method.java:497) ~[?:1.8.0_73]     @ org.apache.tomcat.util.modeler.basemodelmbean.invoke(basemodelmbean.java:300) ~[tomcat-coyote.jar:8.0.32]     @ com.sun.jmx.interceptor.defaultmbeanserverinterceptor.invoke(defaultmbeanserverinterceptor.java:819) ~[?:1.8.0_73]     @ com.sun.jmx.mbeanserver.jmxmbeanserver.invoke(jmxmbeanserver.java:801) ~[?:1.8.0_73]     @ javax.management.remote.rmi.rmiconnectionimpl.dooperation(rmiconnectionimpl.java:1468) ~[?:1.8.0_73]     @ javax.management.remote.rmi.rmiconnectionimpl.access$300(rmiconnectionimpl.java:76) ~[?:1.8.0_73]     @ javax.management.remote.rmi.rmiconnectionimpl$privilegedoperation.run(rmiconnectionimpl.java:1309) ~[?:1.8.0_73]     @ javax.management.remote.rmi.rmiconnectionimpl.doprivilegedoperation(rmiconnectionimpl.java:1401) ~[?:1.8.0_73]     @ javax.management.remote.rmi.rmiconnectionimpl.invoke(rmiconnectionimpl.java:829) ~[?:1.8.0_73]     @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) ~[?:1.8.0_73]     @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) ~[?:1.8.0_73]     @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) ~[?:1.8.0_73]     @ java.lang.reflect.method.invoke(method.java:497) ~[?:1.8.0_73]     @ sun.rmi.server.unicastserverref.dispatch(unicastserverref.java:323) ~[?:1.8.0_73]     @ sun.rmi.transport.transport$1.run(transport.java:200) ~[?:1.8.0_73]     @ sun.rmi.transport.transport$1.run(transport.java:197) ~[?:1.8.0_73]     @ java.security.accesscontroller.doprivileged(native method) ~[?:1.8.0_73]     @ sun.rmi.transport.transport.servicecall(transport.java:196) ~[?:1.8.0_73]     @ sun.rmi.transport.tcp.tcptransport.handlemessages(tcptransport.java:568) ~[?:1.8.0_73]     @ sun.rmi.transport.tcp.tcptransport$connectionhandler.run0(tcptransport.java:826) ~[?:1.8.0_73]     @ sun.rmi.transport.tcp.tcptransport$connectionhandler.lambda$run$256(tcptransport.java:683) ~[?:1.8.0_73]     @ java.security.accesscontroller.doprivileged(native method) ~[?:1.8.0_73]     @ sun.rmi.transport.tcp.tcptransport$connectionhandler.run(tcptransport.java:682) [?:1.8.0_73]     @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1142) [?:1.8.0_73]     @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:617) [?:1.8.0_73]     @ java.lang.thread.run(thread.java:745) [?:1.8.0_73] caused by: org.springframework.beans.factory.beancreationexception: not autowire field: private com.grass.module.sys.dao.userdao com.grass.module.sys.service.userservice.userdao; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [com.grass.module.sys.dao.userdao] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)}     @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredfieldelement.inject(autowiredannotationbeanpostprocessor.java:573) ~[spring-beans-4.2.5.release.jar:4.2.5.release]     @ org.springframework.beans.factory.annotation.injectionmetadata.inject(injectionmetadata.java:88) ~[spring-beans-4.2.5.release.jar:4.2.5.release]     @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeanpostprocessor.java:331) ~[spring-beans-4.2.5.release.jar:4.2.5.release]     ... 58 more caused by: org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [com.grass.module.sys.dao.userdao] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)}     @ org.springframework.beans.factory.support.defaultlistablebeanfactory.raisenosuchbeandefinitionexception(defaultlistablebeanfactory.java:1373) ~[spring-beans-4.2.5.release.jar:4.2.5.release]     @ org.springframework.beans.factory.support.defaultlistablebeanfactory.doresolvedependency(defaultlistablebeanfactory.java:1119) ~[spring-beans-4.2.5.release.jar:4.2.5.release]     @ org.springframework.beans.factory.support.defaultlistablebeanfactory.resolvedependency(defaultlistablebeanfactory.java:1014) ~[spring-beans-4.2.5.release.jar:4.2.5.release]     @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredfieldelement.inject(autowiredannotationbeanpostprocessor.java:545) ~[spring-beans-4.2.5.release.jar:4.2.5.release]     @ org.springframework.beans.factory.annotation.injectionmetadata.inject(injectionmetadata.java:88) ~[spring-beans-4.2.5.release.jar:4.2.5.release]     @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeanpostprocessor.java:331) ~[spring-beans-4.2.5.release.jar:4.2.5.release]     ... 58 more  19-apr-2016 16:26:37.231 severe [rmi tcp connection(2)-127.0.0.1] org.apache.catalina.core.standardcontext.startinternal 1 or more listeners failed start. full details found in appropriate container log file 19-apr-2016 16:26:37.232 severe [rmi tcp connection(2)-127.0.0.1] org.apache.catalina.core.standardcontext.startinternal context [] startup failed due previous errors [2016-04-19 04:26:37,286] artifact gradle : com.grass:grass-web : grass-web-1.0-snapshot.war (exploded): error during artifact deployment. see server log details. 

but use springapplication.run(application.class, args) ok

https://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html#howto-use-spring-data-repositories

spring data can create implementations of @repository interfaces of various flavors. spring boot handle of long @repositories included in same package (or sub-package) of @enableautoconfiguration class.

spring boot tries guess location of @repository definitions, based on @enableautoconfiguration finds. more control, use @enablejparepositories annotation (from spring data jpa).

since webinitializer class doesn't provide configuration class annotated @enableautoconfiguration need add @enablejparepositories annotation config class, e.g. add datasourceconfig.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -