I. Overview
This section focuses on getting the contents of the properties file.
Two. Get the value of an attribute using an environment variable
In Springboot, all the scanned property files are stored in the environment variable, and the environment variable also places some other properties. Therefore, we can get the value of the property file from the environment variable.
Jdbc.username=root
We configured the above content in the Application.properties file.
@SpringBootTest (Classes=springbootrunnerclass. Class) @RunWith (Springrunner. class )publicclass propertiestest { @Resource private Environment env; @Test publicvoid Test () { System. out. println (Env.getproperty ("jdbc.username"));} }
In this way, we can define default values and type conversions, as shown in the following example:
jdbc.port=9999
We add the above content and perform the following tests:
@Test publicvoid test1 () { System. out. println (Env.getproperty ("jdbc.port", Integer. Class)); }
@Test publicvoid test2 () { System. out. println (Env.getproperty ("jdbc.password","trek " )); }
In the second test, we did not configure Jdbc.password, but we used a default value to configure it.
Three. Using @value annotations to complete the acquisition of property values
@Value ("${jdbc.username}") private String username; @Test publicvoid test3 () { System. out . println (username); }
Using this approach, we can still implement type conversions.
@Value ("${jdbc.port}") private Integer port; @Test publicvoid test4 () { System. out . println (port); }
But in this way, it is not possible for us to imagine providing a default value as above.
Four. property file placeholders
Prefix=trek
Name=${prefix}name
In the above configuration, we used the concept of placeholders.
Can the placeholder take effect in the spring environment?
@Value ("${name}") private String name; @Test publicvoid test5 () { System. out . println (name); }
We have found that we can complete the placeholder operation with an expression of ${} in the properties file.
Five. Causing other properties files
In the springboot, we provided the @propertysource to complete.
We first set up a pro property file in the resource directory.
The specific contents are as follows:
User.age=123
user.password=234
Look at our test code:
@Configuration @propertysource (Value="classpath:pro.properties") Public class Proconfig {}
We first create a configuration that introduces our additional configuration files into the environment variables.
@SpringBootTest (classes = Springbootrunnerclass.class) @RunWith (Springrunner.class) Public classPropertiesTest2 {@Value ("${user.age}") PrivateString age; @Value ("${user.password}") PrivateString password; @Test Public voidTest () {System. out. println (age); System. out. println (password); }}
We found that we can now use the external properties file.
Six. Object property mapping
One of our common practices is to map the contents of a property file to a bean. In Springboot, we can use @configurationproperties to complete.
@Configuration @configurationproperties (prefix="user") Public class User { private Integer age; Private String password; Private String language = "Chinese";
In this configuration class, we first @configurationproperties annotations, mapping attributes prefixed with "user" to the configuration class, where we demonstrate three scenarios.
[1] Type conversion
[2] Default value
[3] General Conditions
@Resource private user user; @Test publicvoid test2 () { System. out . println (user); }
001-Properties File