Вопрос:

Spring JavaConfig equivalent of referring to beans by name

java spring

232 просмотра

1 ответ

58 Репутация автора

In Spring XML config, I can use the ref="bean_name" syntax to refer to a bean by ID or name.

<bean id="petStore" class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl">
    <property name="accountDao" ref="accountDao"/>
</bean>

Is there an equivalent way of achieving this in Java config, using the name attribute on the @Bean annotation? I've used the @Named annotation (javax.inject.Named) and have seen @Qualifier suggested elsewhere, but is a separate annotation really necessary if we already have a name attribute on the @Bean annotation?

Автор: praetorian1 Источник Размещён: 03.02.2017 10:16

Ответы (1)


1 плюс

614 Репутация автора

@Qualifier is the correct thing to use here.

@Autowired
@Qualifier("bean_name")
private Beantype var;

And declaring your component

@Component(value = "bean_name")
public class Beantype {...}

At least that works for me.

Автор: Rainer Размещён: 03.02.2017 10:33
Вопросы из категории :
32x32