java - Autowiring a bean based on a property -
i have 2 beans, beana
, beanb
, in spring config. both of these beans implement same interface. have class autowired field of interface type (i.e. populated instance of beana
or beanb
).
initially there 1 bean, used @autowired
annotation , field populated. however, there's 2 potential beans autowired. want autowire bean based on existence of property in 1 of .properties
resources. there elegant way this?
the solution i'm using use @qualifier
annotation on autowired field specify beana
, make check see if property exists in code. if does, reassign field instance of beanb
. it's clunky way of doing it, i'm looking better option.
apart newer feature of bean profiles, can take advantage of factorybean
instantiate bean @ time of access. idea inject factorybean
bean types (e.g. fqcn.beana
or fqcn.beanb
). factory bean return bean factory instantiate correct type of bean may need. configuration of factorybean
can take advantage of properties coming resource bundle.
Comments
Post a Comment