whistling_marmot
whistling_marmot

Reputation: 3883

Partial auto-wire Spring prototype bean with runtime determined constructor arguments

The javadoc to ConstructorResolver.autowireConstructor(...) says

Also applied if explicit constructor argument values are specified, matching all remaining arguments with beans from the bean factory.

but I can't get it to work. I get a BeanCreationException:

Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)

In this example, I have a bean with a constructor that takes Spring beans as well as a String and an int that will only be known at runtime.

@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class BeanWithRuntimeDependencies {

    public final DependencyA dependencyA;
    public final DependencyB dependencyB;
    public final String myString;
    public final int myInt;

    public BeanWithRuntimeDependencies(
            DependencyA dependencyA, DependencyB dependencyB, 
            String myString, int myInt) {
        this.dependencyA = dependencyA;
        this.dependencyB = dependencyB;
        this.myString = myString;
        this.myInt = myInt;
    }

}

@Component
public class DependencyA { /* ... */ }

@Component
public class DependencyB { /* ... */ }

and my test:

@RunWith(SpringRunner.class)
@SpringBootTest
public class PrototypeBeanConstructorsApplicationTests {

    @Autowired private ApplicationContext context;

    @Autowired private DependencyA dependencyA;

    @Autowired private DependencyB dependencyB;

    @Test
    public void getBeanFromContext() {
        BeanWithRuntimeDependencies bean = 
            context.getBean(BeanWithRuntimeDependencies.class, "runtime string", 10);
        assertNotNull(bean);
        assertEquals(dependencyA, bean.dependencyA);
        assertEquals(dependencyB, bean.dependencyB);
        assertEquals("runtime string", bean.myString);
        assertEquals(10, bean.myInt);
    }

}

The source code of ConstructorResolver.autowireConstructor(...) has a comment:

// Explicit arguments given -> arguments length must match exactly.

which seems to contradict its javadoc.

Is it possible to do this? What I am I doing wrong?

Upvotes: 10

Views: 3019

Answers (1)

Ken Bekov
Ken Bekov

Reputation: 14015

Looks like it is not possible to do it the way you're doing.

Actually it is strange situation. According next snippet in ConstructorResolver.autowireConstructor(...)(source code line #207) Spring will not consider your constructor as candidate for invocation:

...    
// Explicit arguments given -> arguments length must match exactly.
if (paramTypes.length != explicitArgs.length) {
    continue;
}

And as you correctly noted it is really contradict to javadoc statement:

...matching all remaining arguments with beans from the bean factory

But anyway implementation means that by default Spring can't resolve constructor for instantiate such beans. And you have to create factory method manually. Something like:

@Configuration
public class Config{

    @Bean
    @Scope(BeanDefinition.SCOPE_PROTOTYPE)
    public BeanWithRuntimeDependencies beanWithRuntimeDependencies(String myString, int myInt){
        return new BeanWithRuntimeDependencies(dependencyA(), dependencyB(), myString, myInt);
    }

    @Bean
    public DependencyA dependencyA(){
        return new dependencyA();
    }

    @Bean
    public DependencyB dependencyB(){
        return new dependencyB();
    }
}

Then you can get bean from context as you want to do:

BeanWithRuntimeDependencies bean = 
    context.getBean(BeanWithRuntimeDependencies.class, "runtime string", 10);

If you don't want to have a deal with configuration class and factory method, you can simply pass needed beans into the context.getBean(). Sure you have to get this beans from context:

BeanWithRuntimeDependencies bean = 
    context.getBean(BeanWithRuntimeDependencies.class, 
        context.getBean(DependencyA.class), 
        context.getBean(DependencyB.class),
        "runtime string", 10);

Upvotes: 8

Related Questions