How to inject List of bean properties by spring SpEL?

I have a properties file for defining dynamic endpoints (name, host and port).

So I created a bean named 'EndPoint' with name, host and port as member variables.

How to inject server's host and port dynamically by using Spring @Value and SpEL?

endpoint.properties

names=server1,server2,server3

endpoint.server1.host=192.168.1.101

endpoint.server1.port=10101

endpoint.server2.host=192.168.1.102

endpoint.server2.port=10102

endpoint.server3.host=192.168.1.103

endpoint.server3.port=10103

Endpoint.java

public class EndPoint {

/** name */

private String name;

// hardcode server1 here

// how to inject server's host dynamically by name?

// @Value("${endpoint.#{this.name}.host}")

@Value("${endpoint.server1.host}")

private String host;

@Value("${endpoint.server1.port}")

private int port;

public EndPoint(String name) {

this.name = name;

}

}

EndPointBeanConfig.java

@Configuration

@PropertySource( //

value = { "classpath:conf/endpoint.properties" }, ignoreResourceNotFound = true//

)

public class EndPointBeanConfig {

@Autowired

private Environment env;

@Bean(name = "endPoints")

public List endPoints() {

final List endPoints =...

Read More »

By: StackOverFlow - Monday, 12 March

Related Posts