Spring Qualifier and Property Placeholder

Spring boot - use a configurable value on a @Qualifier

I ended up adding to the main app class the two implementations autowired then define a bean for each:

@Autowired
TypeOneImpl typeOneImpl
@Bean(name = "typeOneImpl")
public InterfaceRClass getTypeOneImpl()
{
return typeOneImpl;
}

Then in the other class I defined a config field

@Value("${myClass.type}")
private String configClassType;
// the below should be defined in constructor
private final InterfaceRClass interfaceRClassElement ;

And added a setter for it with @Autowired annotation:

@Autowired
public void setMyClassType(ApplicationContext context) {
interfaceRClassElement = (InterfaceRClass) context.getBean(configClassType);
}

In configuration, the value should be typeOneImpl (typeTwoImpl is added for an additional implementation)

Spring properties (property-placeholder) autowiring

You can use @Value:

@Value("${clientapi.url}") 
public void setClientApiUrl(String clientApiUrl) {
this.clientApiUrl = clientApiUrl;
}


Related Topics



Leave a reply



Submit