注意:ide默认对yml的支持不好,安装插件intellij-ansible-0.9.5支持即可,插件下载地址:http://pan.baidu.com/s/1nvgECTN 安装方法这里不赘述, 安装方法参考:https://blog.csdn.net/u014042066/article/details/73826072
myProps: #自定义属性和值 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
package com.mantis; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @Component @ConfigurationProperties( prefix="myProps") //接收application.yml中的myProps下面的属性 public class MyProps { private String simpleProp; private String arrayProps; private List
dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' // spring boot 相关 compile('org.springframework.boot:spring-boot-starter-web') compile('org.springframework.boot:spring-boot-starter-test') compile('org.springframework.boot:spring-boot-starter-jdbc') compile('org.springframework.boot:spring-boot-starter-aop') compile('org.springframework.boot:spring-boot-starter-data-redis') compile('org.springframework.boot:spring-boot-configuration-processor') }
来个小插曲:这里需要说明的是,gradle的注解依赖在中央仓库有支持,只要搜索对应的JAR包就可以了,
当然你要是已经有Maven格式的依赖可以直接复制到build.gradle中,他会自动给你转换成gradle的依赖格式.
但是复制的时候一定要把dependency 一起复制进去
<groupId>junit</groupId> junit artifactId> <version>4.12</version>test scope> </dependency>
有的没有自动转换,可以自己手动转换,对应的格式如下图
package com.mantis; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; //@SpringBootApplication @SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class}) public class ReadYMLApplication { public static void main(String[] args) { SpringApplication.run(ReadYMLApplication.class, args); } }
package com.mantis; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith( SpringJUnit4ClassRunner.class ) @SpringBootTest(classes = ReadYMLApplication.class) public class MyPropsTest { @Autowired private MyProps myProps; @Test //trstReadYmlConfig()方法 public void trstReadYmlConfig(){ System.out.printf("the myProps is : "+myProps.toString()); } }
单元测试结果如下
最后再发个demo地址:https://download.csdn.net/download/master_shifu_/10465336