java - Create multiple beans from one method in @Configuration class -
i spring di , not using xml.
based on configuration (eg. xml/properties file), looking create number of beans (exact number determined configuration) of specific type set in context can autowired classes.
i autowire by: @autowired public myclass(list<mytype> types)
i looking @ using class annotated @configuration
.
so can this:
@configuration public myconfigurationclass { @autowired public void configure(applicationcontext context) { // register stuff here } }
...but doesn't "feel" right...
what "spring" way accomplish this?
edit:
imagine code, to
, ty
empty class definitions.
@configuration public class config { @bean public collection<ty> tylist() { homecoming new arraylist<ty>() {{ this.add(new ty()); // number of ty instances. }}; } @bean public to(collection<ty> tylist) { homecoming new to(); } }
i'm not sure if understood correctly believe want.
public interface mystuff { void dosomething(); } @scope("prototype") // 1 @service("stuffa") // 2 public class mystuffimpl_a implements mystuff { public void dosomething() { // stuff } } @scope("prototype") @service("stuffb") public class mystuffimpl_b implements mystuff { public void dosomething() { // stuff b way ;) } }
now can do:
public class usethestuff { @autowired private provider<mystuff> stuffa; // 3 @autowired private provider<mystuff> stuffb; // 4 public void dostuffwiththeprovider(){ mystuff stuffa.get(); // 5 mystuff stuffb.get(); // 6 } }
tell spring utilize implementation prototype name implementatnion of mystuff: "stuffa" get provider (the name "stuffa" tells spring inject mystuffimpl_a provider) get provider (the name "stuffb" tells spring inject mystuffimpl_b provider) use provider create instance of mystuffimpl_a use provider create instance of mystuffimpl_b java spring
No comments:
Post a Comment