1. 新建工程
2. 点Next
3. 新建如下名字
4. 注意填写自己安装的gradle路径
5. 新建之后的工程时这样的
6. 新建application.yml文件
注意:ide默认对yml的支持不好,安装插件intellij-ansible-0.9.5支持即可,插件下载地址:http://pan.baidu.com/s/1nvgECTN 安装方法这里不赘述, 安装方法参考:https://blog.csdn.net/u014042066/article/details/73826072
7. 在文件中输入以下属性
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
8. 新建java类MyProps.java
9. 增加以下属性,和各自set get方法,可按Alt+insert快速生成
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<Map<String,String>> listProp1=new ArrayList<>(); private List<String>listProp2=new ArrayList<>(); private Map<String,String>mapProps=new HashMap<>(); public void setMapProps(Map<String, String> mapProps) { this.mapProps = mapProps; } public String getSimpleProp() { return simpleProp; } public String getArrayProps() { return arrayProps; } public List<Map<String, String>> getListProp1() { return listProp1; } public List<String> getListProp2() { return listProp2; } public void setSimpleProp(String simpleProp) { this.simpleProp = simpleProp; } public void setArrayProps(String arrayProps) { this.arrayProps = arrayProps; } public void setListProp1(List<Map<String, String>> listProp1) { this.listProp1 = listProp1; } public void setListProp2(List<String> listProp2) { this.listProp2 = listProp2; } public Map<String, String> getMapProps() { return mapProps; } @Override public String toString() { return "MyProps{" + "simpleProp='" + simpleProp + '\'' + ", arrayProps='" + arrayProps + '\'' + ", listProp1=" + listProp1 + ", listProp2=" + listProp2 + ", mapProps=" + mapProps + '}'; } }
10 .接下来解决报错问题,在build.gradle中加上以**解
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 一起复制进去
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>
有的没有自动转换,可以自己手动转换,对应的格式如下图
11.接下来创建启动类SpringBootApplication
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); } }
注意: 如果只用@SpringBootApplication的注解不加上exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class}启动项目时会导致如下报错
启动报错(Cannot determine embedded database driver class for database type NONE) 有兴趣各位客官可以试试
12.导入依赖之后,在MyProps.class文件中按Alt+Enter键去掉报错,没有报红之后按Ctrl+Shift+T创建单元测试类,进行一定的删除与修改,如下
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()); } }
13.最后项目结构如下,点击下面的按钮既可以开始单元测试了,这个教程写的比较详细,适合刚刚入坑gradle
单元测试结果如下
最后再发个demo地址:https://download.csdn.net/download/master_shifu_/10465336