The micro-framework spring-boot has been very popular recently. The author has also followed the trend and said nothing more. Now I will give an example of reading configuration files.
First, you need to rely on the following jar package in the pom file
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
Secondly, we need a spring-boot startup class
@SpringBootApplication @EnableConfigurationProperties({PropsConfig.class,YmlConfig.class}) public class ReadApplication { public static void main(String[] args) { SpringApplication.run(ReadApplication.class, args); } }That's right, the PropsConfig.class and YmlConfig.class pointed out in the @EnableConfigurationProperties annotation are classes that read props and yml configuration files respectively. Next, we will implement the specific implementation of reading properties and yml configuration files respectively.
1. Read properties configuration file
Place an application.properties file under the classpath, with the general content as follows:
master.ds.driverClassName=com.mysql.jdbc.Driver master.ds.url=jdbc:mysql://localhost:3306/test master.ds.username=root master.ds.password=root master.ds.filters=stat master.ds.maxActive=20 master.ds.initialSize=1 master.ds.maxWait=60000 master.ds.minIdle=10 master.ds.timeBetweenEvictionRunsMillis=60000 master.ds.minEvictableIdleTimeMillis=3000000 master.ds.validationQuery=SELECT 'x' master.ds.testWhileIdle=true master.ds.testOnBorrow=false master.ds.testOnReturn=false master.ds.poolPreparedStatements=true master.ds.maxOpenPreparedStatements=100 master.ds.removeAbandoned=true master.ds.removeAbandonedTimeout=1800 master.ds.logAbandoned=true
Reading the props configuration class is very simple. It is basically a pojo/vo class, and just load the @ConfigurationProperties annotation on the class.
@ConfigurationProperties(prefix = "master.ds",locations = "classpath:application.properties") public class PropsConfig { private String driverClassName; private String url; private String username; private String password; private String filters; private String maxActive; private String initialSize; private String maxWait; public String getDriverClassName() { return driverClassName; } public void setDriverClassName(String driverClassName) { this.driverClassName = driverClassName; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String) password) { this.password = password; } public String getFilters() { return filters; } public void setFilters(String filters) { this.filters = filters; } public String getMaxActive() { return maxActive; } public void setMaxActive(String maxActive) { this.maxActive = maxActive; } public String getInitialSize() { return initialSize; } public void setInitialSize(String initialSize) { this.initialSize = initialSize; } public String getMaxWait() { return maxWait; } public void setMaxWait(String maxWait) { this.maxWait = maxWait; } } Unit Test Class
@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = ReadApplication.class) public class ReadApplicationPropsTests { @Autowired private PropsConfig propsConfig; @Test public void testDisplayPropsValue() { String driverClassName = propsConfig.getDriverClassName(); String url = propsConfig.getUrl(); String username = propsConfig.getUsername(); String password = propsConfig.getPassword(); String filters = propsConfig.getFilters(); String maxActive = propsConfig.getMaxActive(); String initialSize = propsConfig.getInitialSize(); String maxWait = propsConfig.getMaxWait(); System.out.println("driverClassName -> " + driverClassName); System.out.println("url -> " + url); System.out.println("username -> " + username); System.out.println("password -> " + password); System.out.println("initialSize -> " + initialSize); System.out.println("maxWait -> " + maxWait); } }You can see the test content output in the console:
driverClassName -> com.mysql.jdbc.Driver url -> jdbc:mysql://localhost:3306/test username -> root password -> root initialSize -> 1 maxWait -> 60000
2. Read the yml configuration file
Place an application.yml file under the classpath, with the general content as follows:
myProps: #Custom properties and values simpleProp: simplePropValue arrayProps: 1,2,3,4,5 listProp1: - name: abc value: abcValue - name: efg value: efgValue listProp2: - config2Value1 - config2Vavlue2 mapProps: key1: value1 key2: value2
Read the class of the yml configuration file.
@ConfigurationProperties(prefix="myProps") //Properties under myProps in application.yml public class YmlConfig { private String simpleProp; private String[] arrayProps; private List<Map<String, String>> listProp1 = new ArrayList<>(); //Receive attribute values in prop1 private List<String> listProp2 = new ArrayList<>(); //Receive attribute values in prop2 private Map<String, String> mapProps = new HashMap<>(); //Receive the attribute value in prop1 public String getSimpleProp() { return simpleProp; } public void setSimpleProp(String simpleProp) { this.simpleProp = simpleProp; } public List<Map<String, String>> getListProp1() { return listProp1; } public List<String> getListProp2() { return listProp2; } public String[] getArrayProps() { return arrayProps; } public void setArrayProps(String[] arrayProps) { this.arrayProps = arrayProps; } public Map<String, String> getMapProps() { return mapProps; } public void setMapProps(Map<String, String> mapProps) { this.mapProps = mapProps; } } Unit Test Class
@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = ReadApplication.class) public class ReadApplicationYmlTests { @Autowired private YmlConfig ymlConfig; @Test public void testDisplayYmlValue() throws JsonProcessingException { System.out.println("simpleProp: " + ymlConfig.getSimpleProp()); ObjectMapper objectMapper = new ObjectMapper(); System.out.println("arrayProps: " + objectMapper.writeValueAsString(ymlConfig.getArrayProps())); System.out.println("listProp1: " + objectMapper.writeValueAsString(ymlConfig.getListProp1())); System.out.println("listProp2: " + objectMapper.writeValueAsString(ymlConfig.getListProp2())); System.out.println("mapProps: " + objectMapper.writeValueAsString(ymlConfig.getMapProps())); } } You can see the test content output in the console:
simpleProp: simplePropValue arrayProps: ["1","2","3","4","5"] listProp1: [{"name":"abc","value":"abcValue"},{"name":"efg","value":"efgValue"}] listProp2: ["config2Value1","config2Vavlue2"] mapProps: {"key1":"value1","key2":"value2"} Isn't it amazing? It can run smoothly without spring's applicationContext.xml file.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.