Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

273
Visualizações
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?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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);
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda