3

In the following Spring Java Config:

@Configuration
@EnableAutoConfiguration
@ComponentScan("my.package")
public class Config {
    @Bean
    public BasicBean basicBean1() {
        return new BasicBean("1");
    }

    @Bean
    public BasicBean basicBean2() {
        return new BasicBean("2");
    }

    @Bean
    public ComplexBean complexBeanByParameters(List<BasicBean> basicBeans) {
        return new ComplexBean(basicBeans);
    }

    @Bean
    public ComplexBean complexBeanByReferences() {
        return new ComplexBean(Arrays.asList(basicBean1(), basicBean2()));
    }
}

I can create two ComplexBeans using either parameter injection, which is elegant, but has shortcomings if a have a few other beans of BasicBean type and only want a few (the parameters can of course be of type BasicBean and enumerate by name the beans I'm interested of, but it could turn out to be a very long list, at least for arguments sake). In case I wish to reference the beans directly I might use the complexBeanByReferences style, which could be useful in case of ordering or some other consideration.

But say I want to use the complexBeanByReference style to reference the bean complexBeanByParameters, that is something along the line of:

@Bean
public ComplexBeanRegistry complexBeanRegistry() {
    return new ComplexBeanRegistry(
        Arrays.asList(
            complexBeanByParameters(), // but this will not work!
            complexBeanByReferences()
        )
    );
}

How would I reference complexBeanByParameters, without having to specify a list of dependencies to complexBeanRegistry? Which, the latter in all honesty should be completely oblivious of.

There is the option to just use

public ComplexBeanRegistry complexBeanRegistry(List<ComplexBeans> complexBeans) {...}

of course, but this might not be an option in certain cases, specifically when using the CacheConfigurer from spring-context. In this case the Java Config is intended to

  1. create the beans
  2. by implementing CacheConfigurer, override the default instances of the CacheManager and KeyGenerator beans.

The requirement to implement CacheConfigurer means I can't change the signature to use parameter injection.

So the question is, is there a way to reference complexBeanByParameters using the "direct" reference style?

1
  • My current solution is to split my configuration in separate files, and by autowiring the bean from one config, injecting it into the CacheConfigurer bean declaration -- the analogue would be to have complexBeanRegistry declared in a separate config and autowire the complexBeanByParameters bean.
    – zrvan
    Commented Feb 10, 2015 at 15:49

1 Answer 1

1

Maybe you could reference it with separation by Qualifier:

    @Bean
    @Qualifier("complexBeanParam")
    public ComplexBean complexBeanByParameters(List<BasicBean> basicBeans) {
        return new ComplexBean(basicBeans);
    }

    @Bean
    @Qualifier("complexBeanRef")
    public ComplexBean complexBeanByReferences() {
        return new ComplexBean(Arrays.asList(basicBean1(), basicBean2()));
    }

and for example autowire:

    @Autowired
    @Qualifier("complexBeanParam")
    private ComplexBean beanParam;
1
  • This does indeed seem to be the closest thing to what I want to do.
    – zrvan
    Commented Mar 3, 2015 at 12:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.