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;
}
Read More:
- IntelliJ idea sets the default working directory
- PostgreSQL sets the default value of the field to the current year
- Idea2021 reports an error. Default operand size is 64 sets the startup task to automatically add the registry
- Compare whether two sets are the same in Java
- Struts 2 encapsulates form data into list and map sets
- MySQL creates tables and sets auto increment of primary keys
- Matplotlib of data visualization plt.xlim The () ylim() function sets the x-axis and y-axis range coordinates
- Latex sets page margin, page size, page margin and geometry macro package
- Default constructor cannot handle exception type FileNotFoundException thrown by implicit super cons
- Modify the default file location of the Jupiter notebook
- Error: field * doesn’t have a default value
- MySQL:ERROR 1067 (42000): Invalid default value for ‘end_time’
- Ubuntu 16.04 modify jupyter default path
- Get the default value of the field
- Error 1364 (HY000): field ‘SSL_ cipher’ doesn’t have a default value
- error: Type of the default value for ‘tableData‘ prop must be a function
- Failed to instantiate [org.springframework.aop.aspectj.AspectJExpressionPointcut]: No default constr
- Error running app: Default Activity Not Found
- Change Jupyter Notebook Default Directory
- Bad default revision ‘head’