Complete the code to inject the property value using @Value annotation.
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class MyComponent { @Value("[1]") private String appName; }
The @Value annotation requires the property key wrapped in ${} to inject the value from application properties.
Complete the code to inject a default value if the property is missing.
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class Config { @Value("[1]") private String serverPort; }
Using ${property:default} syntax in @Value injects the property value or uses the default if missing.
Fix the error in the code to correctly inject an integer property value.
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class ServerConfig { @Value("[1]") private int maxConnections; }
To inject an integer property with a default, use ${property:default} syntax inside @Value.
Fill both blanks to inject a list of strings from a comma-separated property.
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.util.List; @Component public class ListConfig { @Value("[1]") private List<String> servers; public List<String> getServers() { return servers; } }
To inject a list from a comma-separated property, use ${property:default} and split it by comma.
Fill all three blanks to inject a nested property with default and convert it to uppercase.
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class NestedConfig { @Value("[1]") private String env; public String getEnvUpper() { return env.[2]().[3](); } }
Inject the nested property with default, trim spaces, then convert to uppercase.