SpringBoot属性配置

本文详细介绍了 Spring Boot 中 URL 和字段的配置方法,包括如何通过 application.properties 文件配置端口和上下文路径,如何在 Java 类中注入配置值,并演示了使用 @Value 和 @ConfigurationProperties 注解的不同方式。

一: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.其他的配置(数据库)

  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值