码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot属性配置

时间:2017-10-29 22:00:34      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:tor   技术分享   eth   com   factor   ext   images   class   cto   

一:url的配置

1.配置

  默认配置文件是application.properties

  技术分享

 

2.配置

  配置端口

  配置context path

  技术分享

 

3.启动效果

  技术分享

 

 4.第二种配置方式

  要先删除application.properties文件,保留一个即可。

  技术分享

  技术分享

 

5.启动效果

  技术分享

 

二:字段的配置

1.配置文件

  技术分享

 

2.Java文件

 1 package com.caojun.springboot;
 2 
 3 import org.springframework.beans.factory.annotation.Value;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.RequestMethod;
 6 import org.springframework.web.bind.annotation.RestController;
 7 
 8 @RestController
 9 public class HelloSpringBoot {
10 
11     @Value("${cupsize}")
12     private String cupSize;
13 
14     @Value("${age}")
15     private Integer age;
16 
17     @RequestMapping(value="/hello")
18     public String say(){
19         return("cupSize="+cupSize+",age="+age);
20     }
21 }

 

3.启动

  技术分享

 

4.配置中使用配置

  技术分享

 

5.Java文件

package com.caojun.springboot;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloSpringBoot {

    @Value("${content}")
    private String content;

    @RequestMapping(value="/hello")
    public String say(){
        return(content);
    }
}

 

6.启动效果

  技术分享

 

7.简化使用字段的方法,配置文件

  技术分享

 

8.新建Java类

  需要使用Component进行加载bean

  需要使用ConfigurationProperties进行对属性进行配置。

 1 package com.caojun.springboot;
 2 
 3 import org.springframework.boot.context.properties.ConfigurationProperties;
 4 import org.springframework.stereotype.Component;
 5 
 6 @Component
 7 @ConfigurationProperties(prefix = "people")
 8 public class PeoplePerties {
 9     private String name;
10     private Integer age;
11 
12     public String getName() {
13         return name;
14     }
15 
16     public void setName(String name) {
17         this.name = name;
18     }
19 
20     public Integer getAge() {
21         return age;
22     }
23 
24     public void setAge(Integer age) {
25         this.age = age;
26     }
27 }

 

 9.Java程序

 1 package com.caojun.springboot;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.beans.factory.annotation.Value;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.RequestMethod;
 7 import org.springframework.web.bind.annotation.RestController;
 8 
 9 @RestController
10 public class HelloSpringBoot {
11 
12     @Autowired
13     private PeoplePerties peoplePerties;
14 
15     @RequestMapping(value="/hello")
16     public String say(){
17         return peoplePerties.getName()+"====="+peoplePerties.getAge();
18     }
19 }

 

10.启动运行

  技术分享

 

11.多配置文件

  先复制application,然后分别命名:

    技术分享

  修改application.yml:

    技术分享

  dev文件

    技术分享

 

11.运行

  技术分享

 

12.其他的配置(数据库)

  技术分享

 

SpringBoot属性配置

标签:tor   技术分享   eth   com   factor   ext   images   class   cto   

原文地址:http://www.cnblogs.com/juncaoit/p/7751326.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!