当前位置: 首页 > article >正文

SpringBoot开发(四)SpringBoot配置文件

1. SpringBoot配置文件

1.1. 配置端口号和路径

  (1)在application.properties文件下配置端口号和路径。

server.port: 8081
server.servlet.context-path=/demo

在这里插入图片描述
  (2)运行访问。
在这里插入图片描述

1.2. 自定义配置

1.2.1. 方式一

  (1)在application.properties文件下添加配置。

wechat.appid: wx7874d0c23d07e2aa
wechat.token: education
wechat.appSecret: e03f5ab03906d4d943675c233d4c3cb3

在这里插入图片描述

  (2)创建config文件夹和WeChat实体类

package com.zzs.szyj.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties("wechat")
@Component
public class WeChat {

    private String appId;

    private String token;

    private String appSecret;

    private String port;

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getAppSecret() {
        return appSecret;
    }

    public void setAppSecret(String appSecret) {
        this.appSecret = appSecret;
    }
}

在这里插入图片描述
  (3)在HelloController编写新接口

package com.zzs.szyj;

import com.zzs.szyj.config.WeChat;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
// @Controller + @ResponseBody
public class HelloController {
    @Resource
    //@Autowired
    private WeChat weChat;
    @RequestMapping("/hello")
    public String hello() {
        return "hello Spring Boot";
    }
    @GetMapping("/getWeChat")
    public WeChat getWeChat() {
        return weChat;
    }
}

在这里插入图片描述
  (4)重启服务
在这里插入图片描述
  (5)访问接口在这里插入图片描述

1.2.2. 方式二

  (1)创建config文件夹和WeChatConfig类
在这里插入图片描述

@Configuration // 标识这是一个配置类
public class WeChatConfig {
    @Bean
    @ConfigurationProperties("wechat")
    public WeChat weChat() {
        return new WeChat();
    }

}

  (2)重启服务,访问接口
在这里插入图片描述

1.2.3. 方式三

  (1)修改HelloController类
在这里插入图片描述

package com.zzs.szyj;
import com.zzs.szyj.config.WeChat;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
// @Controller + @ResponseBody
public class HelloController {
    //    @Resource
//    //@Autowired
//    private WeChat weChat;
    @Value("${wechat.appId}")
    private String appId;
    @Value("${wechat.token}")
    private String token;
    @Value("${wechat.appSecret}")
    private String appSecret;
    @GetMapping("/getWeChat1")
    public WeChat getWeChat() {
        WeChat weChat = new WeChat();
        weChat.setAppId(appId);
        weChat.setAppSecret(appSecret);
        weChat.setToken(token);
        return weChat;
    }
    @RequestMapping("/hello")
    public String hello() {
        return "hello Spring Boot";
    }
    //    @GetMapping("/getWeChat")
//    public WeChat getWeChat() {
//        return weChat;
//    }

}

  (2)重启服务,访问接口
在这里插入图片描述

1.2.1. 方式四

  @PropertySource
  (1)新建my.properties
在这里插入图片描述

wechat1.appId=wx7874d0c23d07e2aa
wechat1.token=51zxw
wechat1.appSecret=e03f5ab03906d4d943675c233d4c3cb3

  (2)新建MyWeChat类
在这里插入图片描述

package com.zzs.szyj.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@PropertySource("my.properties")
@ConfigurationProperties("wechat1")
@Component
public class MyWeChat {
    private String appId;
    private String token;
    private String appSecret;
    public String getAppId() {
        return appId;
    }
    public void setAppId(String appId) {
        this.appId = appId;
    }
    public String getToken() {
        return token;
    }
    public void setToken(String token) {
        this.token = token;
    }
    public String getAppSecret() {
        return appSecret;
    }
    public void setAppSecret(String appSecret) {
        this.appSecret = appSecret;
    }
}

  (3)修改HelloController类
在这里插入图片描述

package com.zzs.szyj;
import com.zzs.szyj.config.WeChat;
import jakarta.annotation.Resource;
import org.springframework.context.ApplicationContext;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
// @Controller + @ResponseBody
public class HelloController {
    //    @Resource
//    //@Autowired
//    private WeChat weChat;
    @Value("${wechat.appId}")
    private String appId;
    @Value("${wechat.token}")
    private String token;
    @Value("${wechat.appSecret}")
    private String appSecret;
    @Value("${wechat.port}")
    private String wechatPost;
    @Resource
    private ApplicationContext applicationContext;
    @GetMapping("/getWeChat2")
    public WeChat getWeChat2() {
        Environment environment = applicationContext.getEnvironment();
        String appId = environment.getProperty("wechat.appId");
        String token = environment.getProperty("wechat.token");
        String appSecret = environment.getProperty("wechat.appSecret");
        WeChat weChat = new WeChat();
        weChat.setAppId(appId);
        weChat.setPort(wechatPost);
        weChat.setToken(token);
        weChat.setAppSecret(appSecret);
        return weChat;
    }

    @GetMapping("/getWeChat1")
    public WeChat getWeChat() {
        WeChat weChat = new WeChat();
        weChat.setAppId(appId);
        weChat.setAppSecret(appSecret);
        weChat.setToken(token);
        return weChat;
    }
    @RequestMapping("/hello")
    public String hello() {
        return "hello Spring Boot";
    }
    //    @GetMapping("/getWeChat")
//    public WeChat getWeChat() {
//        return weChat;
//    }

}

  (2)重启服务,访问接口
在这里插入图片描述

1.3. application.yml

  (1)新建application.yml
在这里插入图片描述

server:
  port: 8082
wechat:
  appId: wx7874d0c23d07e2aa
  token: 51zxw
  appSecret: e03f5ab03906d4d943675c233d4c3cb3

  (2)重启服务,访问接口
在这里插入图片描述

1.4. SpringBoot多环境配置

在这里插入图片描述
在这里插入图片描述

1.4.1. 修改application.yml

在这里插入图片描述

server:
  port: 8082
wechat:
  appId: wx7874d0c23d07e2aa
  token: 51zxw
  appSecret: e03f5ab03906d4d943675c233d4c3cb3
  port: ${server.port}

在这里插入图片描述

1.4.2. 多环境配置

  (1)新建application-dev.yml、application-local.yml、application-pre.yml、application-prod.yml、application-test.yml
在这里插入图片描述
  (2) 修改application.yml
在这里插入图片描述

wechat:
  appId: wx7874d0c23d07e2aa
  token: 51zxw
  appSecret: e03f5ab03906d4d943675c233d4c3cb3
  port: ${server.port}
spring:
  profiles:
    active: local

  (3) 运行
在这里插入图片描述

1.4.3. 多环境配置注入url

  (1)多环境配置文件注入url
在这里插入图片描述
在这里插入图片描述
  (2)添加链接
在这里插入图片描述
  (2)运行
在这里插入图片描述

  SpringBoot配置文件、配置示例下载


http://www.kler.cn/a/533238.html

相关文章:

  • DeepSeek最新图像模型Janus-Pro论文阅读
  • LeetCode 0922.按奇偶排序数组 II:O(1)空间复杂度-一次遍历双指针
  • 【Java】位图 布隆过滤器
  • Web - CSS3浮动定位与背景样式
  • react-bn-面试
  • 集合通讯概览
  • 【数据可视化-14】Boss招聘数据分析岗位的可视化分析
  • C++11详解(二) -- 引用折叠和完美转发
  • 使用C++构建一个优先级队列
  • 代理模式的作用
  • 从 DeepSeek R1 中提取数学推理数据,使用 CAMEL
  • 华为手机nova9,鸿蒙系统版本4.2.0.159,智慧助手.今天版本是14.x,如何卸载智慧助手.今天?
  • Python进行模型优化与调参
  • SGlang 专为大模型设计的高效服务框架
  • DRGDIP 2.0时代下基于PostgreSQL的成本管理实践与探索(上)
  • AI透明化与全球政治格局的发展:如何避免AI被人为操控
  • 电商用户画像数据可视化分析
  • 基于MODIS/Landsat/Sentinel/国产卫星遥感数据与DSSAT作物模型同化的作物产量估算
  • 使用 Redisson 实现分布式并发限流
  • Spring 面试题【每日20道】【其三】
  • 力扣73矩阵置零
  • 【Leetcode 每日一题】541. 反转字符串 II
  • Vue3 完整学习笔记 - 第二部分
  • Vue.js组件开发-实现广告图片浮动随屏幕滚动
  • LeetCode:115.不同的子序列
  • C++实现有限元三维杆单元计算 Bar3D2Node类(纯自研 非套壳)