java - Why is spring not injecting using generic qualifiers? -


this configuration class

@configuration class factories {      @bean     collection<log> logs() {         list<log> logs = new arraylist<>();         ( int = 0; < 10; i++ ) { // gross example code             log log = new log();             log.setentry( randomstringutils.randomalphabetic( 10 ) );             logs.add( log );         }         return logs;     } } 

and here's how i'm trying autowire it

@service @transactional public class logservice {      private final logrepository repository;     private final objectfactory<instant> now;     private final collection<log> logs;      @autowired     logservice( final logrepository repository, final objectfactory<instant> now, final collection<log> logs ) {         this.repository = repository;         this.now = now;         this.logs = logs;     } 

but exception

caused by: org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [com.xenoterracide.example.log.log] found dependency [collection of com.xenoterracide.example.log.log]: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {} @ org.springframework.beans.factory.support.defaultlistablebeanfactory.raisenosuchbeandefinitionexception(defaultlistablebeanfactory.java:1326) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.doresolvedependency(defaultlistablebeanfactory.java:1024) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.resolvedependency(defaultlistablebeanfactory.java:967) @ org.springframework.beans.factory.support.constructorresolver.resolveautowiredargument(constructorresolver.java:813) @ org.springframework.beans.factory.support.constructorresolver.createargumentarray(constructorresolver.java:741) ... 24 more 

according documentation think should work. here's full code in case need it. misunderstanding documentation?

given @autowired list<something>, spring many something beans have defined in applicationcontext , attempt autowire them target. the documentation states

as specific consequence of semantic difference, beans defined collection or map type cannot injected through @autowired, because type matching not applicable them. use @resource such beans, referring specific collection or map bean unique name.

in case, have

@autowired logservice(/* ... */ final collection<log> logs ) { 

but no log beans. complains

caused by: org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [com.xenoterracide.example.log.log] found dependency [collection of com.xenoterracide.example.log.log]: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {} 

what seem want bean of type collection<log> injected directly. spring can javax.annotation.resource annotation. unfortunately, annotation doesn't work on constructors.

you'll need either annotate (changed non-final) field or add setter method , annotate that.

@resource private collection<log> logs; 

or

@resource public void setlogs(collection<log> logs) {     this.logs = logs; } 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -