SpringBoot-Properties

SpringBoot Demo 获取配置文件属性

一、项目目录结构

在这里插入图片描述

二、配置文件

1、主配置文件:application.yml

server:
  port: 8080 #端口
  servlet:
    context-path: /properties #访问路径
spring:
  profiles:
    active: dashu #配置环境

2、环境配置文件:application-dashu.yml

#配置环境参数
pom:
  name: @artifactId@
  version: @version@
  packaging: @packaging@
user:
  name: dashu
  website: https://blog.csdn.net/weixin_43521890?spm=1000.2115.3001.5113
  email: dashuplus@163.con

3、额外的spring配置元数据:additional-spring-configuration-metadata.json

{
  "properties": [
    {
      "name": "pom.name",
      "description": "Default value is name in pom.xml.",
      "type": "java.lang.String"
    },
    {
      "name": "pom.version",
      "description": "Default value is version in pom.xml.",
      "type": "java.lang.String"
    },
    {
      "name": "pom.packaging",
      "description": "Default value is packaging in pom.xml.",
      "type": "java.lang.String"
    }
  ]
}

4、maven依赖文件:pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.dashu</groupId>
    <artifactId>properties</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>properties</name>
    <packaging>jar</packaging>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--
		在 META-INF/additional-spring-configuration-metadata.json 中配置
		可以去除 application.yml 中自定义配置的红线警告,并且为自定义配置添加 hint 提醒
		 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <!--Lombok能以简单的注解形式来简化java代码,提高开发人员的开发效率。-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!--Hutool是一个小而全的Java工具类库,
        通过静态方法封装,降低相关API的学习成本,
        提高工作效率,使Java拥有函数式语言般的优雅,
        让Java语言也可以“甜甜的”。-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>4.5.11</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

三、项目文件

1、启动类:PropertiesApplication.java

package com.dashu.properties;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * <p>
 * SpringBoot启动类
 * </p>
 *
 * @package: com.dashu.properties
 * @description: SpringBoot启动类
 * @author: dashu
 * @date: Created in 2020/11/19
 * @copyright: Copyright (c)
 * @version: V1.0
 * @modified: dashu
 */
@SpringBootApplication
@Slf4j
public class PropertiesApplication {

    public static void main(String[] args) throws UnknownHostException {
        ConfigurableApplicationContext application = SpringApplication.run(PropertiesApplication.class, args);
        Environment env = application.getEnvironment();
        //获取本机ip
        String ip = InetAddress.getLocalHost().getHostAddress();
        //获取配置文件端口
        String port = env.getProperty("server.port");
        //获取配置文件项目启动路径
        String path = env.getProperty("server.servlet.context-path");
        //日志输出访问路径
        log.info("\n----------------------------------------------------------\n\t" +
                "Application helloword is running! Access URLs:\n\t" +
                "URL: \t\thttp://"+ip+":" + port + path + "\n\t" +
                "----------------------------------------------------------");
    }

}

2、配置实体类

(1)YmlProperty.java
package com.dashu.properties.entity;


import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * <p>
 * yml主配置属性实体类
 * </p>
 *
 * @package: com.dashu.properties.entity
 * @description: 主配置属性
 * @author: dashu
 * @date: Created in 2020/11/19
 * @copyright: Copyright (c)
 * @version: V1.0
 * @modified: dashu
 */
@Data
@Component
public class YmlProperty {

    @Value("${user.name}")
    private String name;

    @Value("${user.website}")
    private String website;

    @Value("${user.email}")
    private String email;
}

(2)PomProperty.java
package com.dashu.properties.entity;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * <p>
 * pom.xml依赖属性实体类
 * </p>
 *
 * @package: com.dashu.properties.entity
 * @description: 依赖属性
 * @author: dashu
 * @date: Created in 2020/11/19
 * @copyright: Copyright (c)
 * @version: V1.0
 * @modified: dashu
 */
@Data
@Component
@ConfigurationProperties(prefix = "pom")
public class PomProperty {

    private String name;

    private String version;

    private String packaging;


}

3、访问控制类:PropertyController.java

package com.dashu.properties.controller;

import cn.hutool.core.lang.Dict;
import com.dashu.properties.entity.PomProperty;
import com.dashu.properties.entity.YmlProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 访问控制类
 * </p>
 *
 * @package: com.dashu.properties.controller
 * @description: 访问控制类
 * @author: dashu
 * @date: Created in 2020/11/19
 * @copyright: Copyright (c)
 * @version: V1.0
 * @modified: dashu
 */
@RestController
@Slf4j
public class PropertyController {

    private final PomProperty pomProperty;

    private final YmlProperty ymlProperty;

    /**
     * 构造方法注入组件
     * @param pomProperty
     * @param ymlProperty
     */
    @Autowired
    public PropertyController(PomProperty pomProperty, YmlProperty ymlProperty) {
        this.pomProperty = pomProperty;
        this.ymlProperty = ymlProperty;
    }

    @GetMapping("getProperty")
    public Dict property(){
        log.info("pom:"+ pomProperty);
        log.info("yml:"+ ymlProperty);
        return Dict.create().set("pom", pomProperty).set("yml", ymlProperty);
    }

}

四、运行

1、项目启动

在这里插入图片描述

2、访问接口

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

3、返回数据格式化

在这里插入图片描述

五、诗词欣赏

			  蜀道难
			[] 李白
噫吁嚱,危乎高哉!蜀道之难,难于上青天!
蚕丛及鱼凫,开国何茫然!
尔来四万八千岁,不与秦塞通人烟。
西当太白有鸟道,可以横绝峨眉巅。
地崩山摧壮士死,然后天梯石栈相钩连。
上有六龙回日之高标,下有冲波逆折之回川。
黄鹤之飞尚不得过,猿猱欲度愁攀援。
青泥何盘盘,百步九折萦岩峦。
扪参历井仰胁息,以手抚膺坐长叹。
问君西游何时还?畏途巉岩不可攀。
但见悲鸟号古木,雄飞雌从绕林间。
又闻子规啼夜月,愁空山。
蜀道之难,难于上青天,使人听此凋朱颜!
连峰去天不盈尺,枯松倒挂倚绝壁。
飞湍瀑流争喧豗,砯崖转石万壑雷。
其险也如此,嗟尔远道之人胡为乎来哉!
剑阁峥嵘而崔嵬,一夫当关,万夫莫开。
所守或匪亲,化为狼与豺。
朝避猛虎,夕避长蛇;磨牙吮血,杀人如麻。
锦城虽云乐,不如早还家。
蜀道之难,难于上青天,侧身西望长咨嗟!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

大树下躲雨

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值