Springboot的使用起始之为属性赋值(笔记)

一、创建项目
在这里插入图片描述
二、创建一个RedisController类

package com.jt.controller; 

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

@RestController
public class RedisController {


//    private String host="127.0.0.1";
//    private int port =26251;

//需求,能否简化修改环节
                            //spring将数据扫描到容器中时才能取值
    @Value("${redis.host}")  //spel表达式
    private String host;
    @Value("${redis.port}")
    private int port;


    @RequestMapping("/getNode")
    public String getNode(){
        return host+"|"+port;
    }
}

由于 private String host=“127.0.0.1”;是写死在程序,不利于维护,所以基于业务分析,可把以后需要提取拿到的数据放到配置文件,在配置文件中为属性赋值,复制后,然后又怎么从配置文件中取得这些值?我们通过@Value(${ })获取拿到在这里插入图片描述
以上是我们通过xxx.yml配置文件获取拿到数据。

下面我们还可以通过xxx.properties配置文件关联拿到数据,首先可以复制一份上面类,做一些访问路径等的修改。

package com.jt.controller;

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

@RestController
//当程序启动时加载指定资源
@PropertySource(
value = "classpath:/properties/redis.properties",encoding = "utf-8")

public class RedisController2 {


    @Value("${redis.host2}")
    private String host;
    @Value("${redis.port2}")
    private int port;


    @RequestMapping("/getNode2")
    public String getNode(){
        return host+"|"+port;
    }
}

上面的@PropertySource(
value = “classpath:/properties/redis.properties”,encoding = “utf-8”),由于idea设置里面的utf-8只是负责页面的显示编码,而取出来获取的数据会出现中文乱码,所以我们可以通过这对页面显示的编码方式,解决中文乱码问题。
对于这之间需要如何关联?
@Value(" r e d i s . h o s t 2 " ) 这 里 通 过 @ V a l u e ( " {redis.host2}") 这里通过@Value(" redis.host2")@Value("{redis.host2}") 获取到值。
private String host;
@RequestMapping("/getNode2") 为请求映射的路径
而对于xxx.properties中设置属性值需要:在这里插入图片描述
## 通过xxx.yml配置时,为了利于维护,我们可以配置环境切换,通过—分割环境
#设置默认配置环境

spring:
  profiles:
    active: dev

---
#语法  1.key:(空格)value
#2.yml文件有层级关系
#3.默认utf-8格式编码

#标识配置信息
#破解版新版本写法
spring:
  config:
    activate:
      on-profile: dev


server:
  port: 8090

#为属性赋值通过yml
redis:
  host: 127.0.0.20
  port: 9995

#环境切换
---  #实现配置文件拆分
spring:
  config:
    activate:
      on-profile: prod   
   #prod 为环境取一个名字,便于上面设置默认环境的调用,也就是使用哪一个环境
server:
  port: 8088

redis:
  host: 192.168.1.12
  port: 4525

对于新版本,设置默认配置环境哪儿写法,active是用于选择使用哪一个环境,冒号后面的是 on-profile: dev的名字,破解版传统写法有一个下划线,这不多举例。对于多个环境,我们得使用一种标识用于去区分不同的环境,于是在每个属性赋值前面都有上面名字那段标识,然后到时候在默认配置环境哪儿选择有关标识段。启动,打开游览器,输入地址加路径,访问。

以上就是通过xxx.yml方式或者xxx.properties为属性赋值方式。

Spring Boot项目中,启动后会首先在静态资源路径(resources/static)下查找index.html作为首页文件。 这意味着如果您在该位置下创建了名为index.html的文件,它将作为您的应用程序的起始页。 另外,如果您需要在开发过程中启用自动重启功能,您可以在配置文件中添加spring.devtools.restart.additional-paths属性,并指定需要监视重启的其他目录路径。这将使您在对这些目录下的文件进行更改后,应用程序可以自动重新启动。 最后,如果您想要跳过Spring核心配置搜索BeanInfo,您可以在配置文件中添加spring.autoconfigure.exclude属性,并指定需要排除的。这可以帮助您提高应用程序的启动速度。 请注意,这个属性是可选的,只在您有特定需求的情况下才需要使用。 总而言之,通过在静态资源路径下创建index.html文件作为首页,并根据需要配置自动重启和排除Spring核心配置搜索BeanInfo,您可以轻松配置Spring Boot的起始页。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [初识SpringBoot——设置SpringBoot应用默认首页](https://blog.youkuaiyun.com/chenshufeng115/article/details/108370736)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [springboot配置文件分](https://blog.youkuaiyun.com/qq_43255308/article/details/102896055)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值