java - How define the dependency injections in spring-boot? -
i try create spring web application spring-boot. first problem dependency injection not work me. article followed.
i created application class:
@springbootapplication public class application extends springbootservletinitializer { @override protected springapplicationbuilder configure(springapplicationbuilder application) { return application.sources(application.class); } public static void main(string[] args) throws exception { springapplication.run(application.class, args); } }
then made controller:
@restcontroller public class greetingcontroller { @autowired wfexampledao wfexampledao; private static final string template = "hello, %s!"; private final atomiclong counter = new atomiclong(); @requestmapping("/greeting") public greeting greeting(@requestparam(value="name", defaultvalue="world") string name) { wfstartdao.insert(null); return new greeting(counter.incrementandget(), string.format(template, name)); } }
my exampledao:
public interface wfexampledao { public void insert(wfexample wfexample); }
and implementation of interface:
@component public class wfexampledaoimpl extends jdbcdaosupport implements wfexampledao { private logger logger; public wfexampledaoimpl() { this.logger = loggerfactory.getlogger(this.getclass()); } @override public void insert(wfexample wfexample) { logger.info("insert not implemented yet."); } }
my gradle file:
buildscript { repositories { mavencentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.6.release") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'idea' apply plugin: 'org.springframework.boot' apply plugin: 'war' war { basename = 'gs-rest-service' version = '0.1.0' } jar { basename = 'gs-rest-service' version = '0.1.0' } repositories { mavencentral() } sourcecompatibility = 1.8 targetcompatibility = 1.8 dependencies { compile group: 'org.springframework', name: 'spring-context', version: '4.3.11.release' compile 'org.springframework:spring-jdbc:4.3.11.release' compile 'commons-dbcp:commons-dbcp:1.4' compile 'commons-pool:commons-pool:1.6' compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.5.6.release' providedruntime group: 'org.springframework.boot', name: 'spring-boot-starter-tomcat', version: '1.5.6.release' testcompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '1.5.6.release' }
what expect: when open /greeting page, log appear, got in begining of gradle bootrun
:
2017-09-12 10:47:56.058 warn 7545 --- [ main] ationconfigembeddedwebapplicationcontext : exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.unsatisfieddependencyexception: error creating bean name 'greetingcontroller': unsatisfied dependency expressed through field 'wfexampledao'; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type 'hu.example.dao.wfexampledao' available: expected @ least 1 bean qualifies autowire candidate. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)}
i don't know why can not find dependency. read, don't have create applicationcontext.xml
because bootrun figures out dependencies.
thanks advices in advance!
at default, springboot scan components in child packages of application class. didn't specify component scan make sure classes in same package or child packages of application
.
Comments
Post a Comment