Tag Archives: @Value static

@Value sets the default value

When using @ value annotation to automatically inject variables, a common problem is that we may forget to set the parameter in the configuration parameter, resulting in an error in the whole project. In fact, we can set a default value when injecting the @ value annotated variable. If the configuration parameter is not found, it is equivalent to a way to get the bottom of the story Case No

Usage without default value:

@Value("${spring.port}")
private String port;

To increase the default value:

@Value("${spring.port:8080}")
private String port;

Here we need to pay attention to:

Even if the value of your configuration parameter is string type, you don’t need to add double quotation marks. Static modified variables will fail to be injected through @ value

If it is necessary to set the value of @ value to the static variable due to specific requirements, the following method can be used

Use @ value to set the value of static variable

public static String port;

@Value("${spring.port:8080}")
public void setPort(String port){
	Class.port = port;
}