java - Injecting object into Spring Configuration -


i turning old xml/java configuration pure java config. in xml used injection of parameters configuration file this:

<bean class="com.project.springrestconfiguration">     <property name="parameters" ref="parameters" /> </bean>    @configuration public class springrestconfiguration {      private parameters parameters;      public void setparameters(parameters parameters) {         this.parameters = parameters;     }      // @bean definitions     ... } 

is possible inject parameters in javaconfig? (without need of using autowiring!)

@configuration @import(springrestconfiguration.class) 

edit: @import can't see chance inject parameters springrestconfiguration

basically need use @autowired can still use name , not type interpretation this:

@configuration public class springrestconfiguration {      @autowired     @qualifier("parameters") // somewhere in context should have bean named 'parameters'. doesn't matter if defined xml, configuration class or auto scanning. long such bean right type , name exists, should good.     private parameters parameters;      // @bean definitions     ... } 

this solves confusion problem mentioned when using @autowired - there's no question here bean injected, bean named parameters.

you can little test, leave parameters bean defined in xml before, use @autowired, see works. migrate parameters @configuration class.

in answer here can find complete explanation of how should migrate xml @configuration step step.

you can skip private member altogether , this:

@configuration public class springrestconfiguration {      @bean     public beanthatneedsparamters beanthatneedsparamters (@qualifier("parameters") parameters parameters) {        return new beanthatneedsparamters(parameters)     }  } 

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -