设为首页 加入收藏

TOP

Spring Boot实现任意位置的properties及yml文件内容配置与获取(一)
2023-07-25 21:35:30 】 浏览:88
Tags:Spring Boot 任意位 properties yml 文件内 容配置

〇、参考资料

1、Spring Boot 中文乱码问题解决方案汇总

https://blog.51cto.com/u_15236724/5372824

2、spring boot读取自定义配置properties文件★

https://www.yisu.com/zixun/366877.html

3、spring boot通过配置工厂类,实现读取指定位置的yml文件★

https://blog.csdn.net/weixin_45168162/article/details/125427465

4、springBoot 读取yml 配置文件的三种方式(包含以及非component下)★

https://blog.csdn.net/weixin_44131922/article/details/126866040

5、SpringBoot集成Swagger的详细步骤

https://blog.csdn.net/m0_67788957/article/details/123670244

一、项目介绍

1、项目框架

 

2、技术栈

 Spring Boot+Swagger+Lombok+Hutool

3、项目地址

 https://gitee.com/ljhahu/kettle_processor.git

需要权限请联系:liujinhui-ahu@foxmail.com

二、properties配置与使用

1、默认配置文件

(1)配置-application.properties

# Spring Boot端口配置
server.port=9088
# spring数据源配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.password=qaz123
spring.datasource.username=root
spring.datasource.url=jdbc:mysql://192.168.40.111:3306/visualization?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.thymeleaf.prefix=classpath:/templates/

(2)读取

Spring Boot自己会读取,配置数据源、thymeleaf等信息

 

2、自定义配置文件

(1)配置-kettle.properties

# properties  https://blog.csdn.net/weixin_42352733/article/details/121830775
environment=xuelei-www
kettle.repository.type=database
kettle.repository.username=admin
kettle.repository.password=admin

(2)使用-读取单个值-PropertiesController.java

package com.boulderaitech.controller;

import com.boulderaitech.entity.KettleRepositoryBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
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;

@Api("properties测试")
@RestController //Controller和RestController的区别
@PropertySource("classpath:kettle.properties") //默认是application.properties
//可以将PropertySource注解加入entity,也可以加入controller将bean注入
public class PropertiesController {
    @Value("${environment}")
    private String envName;

    @Autowired
    private KettleRepositoryBean kettleRepositoryBean;

    @RequestMapping("/getEnv")
    @ApiOperation("properties方式获取当前的环境")
    public String getEnv() {
        return "hello " + envName;
    }
    @ApiOperation("properties方式获取当前的环境")
    @RequestMapping("/getRepoInfo")
    public String getRepoInfo() {
        return "hello " + kettleRepositoryBean.toString();
    }
}

(3)使用-读取多个值到对象-KettleRepositoryBean.java

package com.boulderaitech.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@NoArgsConstructor
@AllArgsConstructor
@ConfigurationProperties(prefix = "kettle.repository")
public class KettleRepositoryBean {
    private String typ
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇十二条后端开发经验分享,纯干货.. 下一篇面试官:如何保证接口幂等性?一..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目