Application.properties Everyone is not strange, when we are developing, we often use it to configure some variables that can be manually modified and not compiled, the effect is that when the war package or jar for the production environment, we can manually modify the environment variables without recompiling.
Spring Boo has already configured many environment variables by default, for example, Tomcat's default port is 8080, the project's ContextPath is "/", and so on, you can see Spring boot default configuration information here http://docs.spring.io/ spring-boot/docs/current-snapshot/reference/htmlsingle/#boot-features-external-config
Spring boot allows you to customize a application.properties file and then place it in the following places to override the spring boot environment variables or define your own environment variables
- Subdirectories of "/config" in the current directory
- Under current directory
- Classpath the root directory under the "/config" package
- The root directory of the Classpath
1-point and 2-point fit in a production environment, for example, packaged into an executable jar package
Note here that the "current directory" refers to the directory of the Demo.jar package, in order for the configuration file to take effect, when using the Java-jar Demo.jar command, you must first route to the Demo.jar package path, and then use its name,
3-point and 4-point fit in the development environment
If you have a configuration file in four places at the same time, the profile is prioritized from 1 to 4.
With the configuration file, when Spring Boo starts, it automatically reads the configuration information into the spring container and overrides the default configuration of spring boot, so how do we read and set these configuration information?
1. Override and configure environment variables by command line with the highest priority, for example, you can override the spring boot embedded Tomcat service port with the following command, noting that there are no spaces between the two sides
java -jar demo.jar --server.port=9000
If you want to set multiple variables, you can set the JSON format string
demo.jar --spring.application.json=‘{"foo":"bar"}‘
2. Read by @value annotations
@RestController@RequestMapping("/task")public class TaskController {@Value("${connection.remoteAddress}") private String address;@RequestMapping(value = {"/",""})public String hellTask(@Value("${connection.username}")String name){ return "hello task !!";}}
3. Through the environment interface to obtain, only need to put the interface in the
@RestController @ Requestmapping ( "/task") public Class Taskcontroller { @Autowired environment ev; "${connection.remoteaddress}") private string address ; @RequestMapping (value = { "}") public String helltask ( @Value ( "$ {connection.username} ") string name {string password = Ev.getproperty (" Connection.password "); return "Hello task!";}
4. A tool class can be customized to obtain, the key is to read the configuration file information, suitable for custom configuration information, the spring container default configuration information will not be read
@ComponentPublicClassSystemconfig {Privatestatic Properties props;PublicSystemconfig () {try {Resource Resource =New Classpathresource ("/application.properties");Props = propertiesloaderutils.loadproperties (Resource); }catch (IOException e) {e.printstacktrace ();}}/** * Get Properties *@param key *@return */PublicStatic StringGetProperty (String key) {return props = =Null?Null:props.getProperty (key); }/** * Get Properties * @param key Property key * @param defaultvalue property value * @return */public static string getproperty (String key, String defaultvalue) {return props = null? null:props.getproperty (key, DefaultValue); } /** * get properyies properties * @return */ public static Properties GetProperties () {return props;}} //with the words, just like this string value = Systemconfig.getproperty ( "key");
5. Can take advantage of ${...} Referencing variables in application.properties
myapp.name=springmyapp.desc=${myapp.name} nice
6. You can configure random variables in application.properties, using the Randomvaluepropertysource class
my.secret=${random.value}my.number=${random.int}my.bignumber=${random.long}my.number.less.than.ten=${random.int(10)}my.number.in.range=${random.int[1024,65536]}
The use of simple configuration files is first written here, and then look at other advanced usage, such as profiles and @configurationproperties
Spring Boot uses application.properties for external configuration