ESTRELLA HUANG
ESTRELLA HUANG

Reputation: 1

Cannot resolve @Value("${xxx:}") (Spring 4) but can resolve @Value("${xxx}")

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class ServiceTest {
    public static String coreIp;
    public static String corePort;

    @Value("${core_ip}")
    private void setcoreIp(String coreIp) {
    ServiceTest.coreIp = coreIp;
    }
    @Value("${core_port:}")
    private void setcorePort(String corePort) {
    ServiceTest.corePort = corePort;
    }
}

I can get the value of coreIp, but I cannot get the value of corePort. This code exists in Spring. When I remove ':' in @Value, it works. When I move this code to Spring MVC ,it can work and I can get both value.

Is there any difference between Spring and SpringMVC when they resolve @Value.

Any suggestion?

Upvotes: 0

Views: 322

Answers (1)

Branislav Lazic
Branislav Lazic

Reputation: 14816

The problem is that SpEL ${core_port:} expects to provide default value for core_port property. Therefore, either provide a default value for core_port (e.g. ${core_value:3000}) or rename your property by removing semicolon.

Edit:

Since your core_port is String type, then the value will be an empty string.

Upvotes: 2

Related Questions