配置文件Application.properties

属性配置

在application.properties中添加server.port=端口号即可

在这里插入图片描述

# 服务器端口配置
server.port=80

# 修改banner  关闭banner
spring.main.banner-mode=off

# 输出日志
logging.level.root=info

配置文件的多种格式

在这里插入图片描述

主流格式:yml

加载优先级

在这里插入图片描述

yaml的数据格式

在这里插入图片描述

数据前面要有空格进行隔开

server:
  port: 80

users:
  name: itheima
  age: 16

users1:
  - name: ss
    age: 11

  - name: ew
    age: 21

读取yaml文件中的属性值

在controller使用value注解为变量注入值

    @Value("${country}")
    private String country1;

//     该注解定义Get请求  访问方式

//    GetMapping整合请求路径与请求方式
    @GetMapping
    public String getById(){
        System.out.println("spingboot is running");
        System.out.println(country1);
        return "springboot is running...";
    }


yaml文件引用别的值

country: China
city: ${country}hefei

读取yaml文件中的全部属性

使用自动装配将所有的配置添加到一个Environment对象中

package com.ustc.sp7.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;

import javax.xml.stream.events.EndElement;

// 第一个注解 满足restful风格的开发模式
// 第二个注解  定义访问请求路径
@RestController
@RequestMapping("/books")
public class BookController {

    @Autowired
    private Environment env;// 自动加载所有的属性

//     读取yaml文件中属性值
    @Value("${country}")
    private String country1;

    @Value("${city}")
    private String city1;

//     该注解定义Get请求  访问方式

//    GetMapping整合请求路径与请求方式
    @GetMapping
    public String getById(){
        System.out.println("spingboot is running");
//        System.out.println(country1);
//        System.out.println(city1);
//        System.out.println(city1);
//
        System.out.println(env.getProperty("country"));
        return "springboot is running...";
    }

    // value定义 路径  method定义访问的方式
    @RequestMapping(value = "/users",method = RequestMethod.POST)
    @ResponseBody
    public String save(){
        System.out.println("user save...");
        return "{'module':'user save'}";
    }

    @RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public String delete(@PathVariable Integer id){
        System.out.println("user delete ..." + id);
        return "{'module':'user delete'}";
    }
}

yaml文件 数据库的属性

  • 定义数据模型封装yaml文件中对应的数据
  • 定义为spring管控的Bean Component注解
  • 指定加载的数据

首先,在yaml文件中将数据提前定义好,然后定义一个类-》数据模型封装这些数据

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

少写代码少看论文多多睡觉

求打赏,求关注,求点赞

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值