ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Spring Boot 配置

2022-07-24 08:00:40  阅读:157  来源: 互联网

标签:Spring 配置 Boot springframework application cunyu import org properties


1. 前言

为了 Spring Boot 能够更好地生成配置元数据文件,我们可以在创建项目时添加 Spring Configuartion Processor 依赖,或者在创建好项目后的 pom.xml 文件中手动添加。添加该依赖后,我们在编写配置时就会有属性提示,大大降低编写错误。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

2. application.properties

2.1 自定义属性

application.properties 配置文件是创建项目后就自带的,如果我们要自定义属性,可以在其中直接配置,配置过程如下:

  1. 在 application.properties 中添加我们要自定义的配置;
cunyu.id=1024
cunyu.name=村雨遥
cunyu.website=https://cunyu1943.github.io
  1. 创建实体类来映射我们配置的属性;
package com.cunyu.pojo;

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

/**
 * @author : cunyu
 * @version : 1.0
 * @className : CunyuProperties
 * @date : 2020/7/29 13:34
 * @description : TODO
 */

@Component
@ConfigurationProperties(prefix = "cunyu")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CunyuProperties {
    private int id;
    private String name;
    private String website;
}
  1. 定义 Controller 来注入测试;
package com.cunyu.controller;

import com.cunyu.pojo.CunyuProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;

/**
 * @author : cunyu
 * @version : 1.0
 * @className : CunyuPropertiesController
 * @date : 2020/7/29 13:37
 * @description : TODO
 */

@RestController
@RequestMapping("/cunyu")
public class CunyuPropertiesController {
    private static final Logger logger = LoggerFactory.getLogger(CunyuPropertiesController.class);

    @Autowired
    CunyuProperties cunyuProperties;

    @GetMapping("/profile")
    public String cunyuProfile(){
        logger.info("---------------");
        logger.info(cunyuProperties.toString());
        logger.info("---------------");
        return cunyuProperties.toString();
    }
}
  1. 打开网页测试,打开 1,同时观察控制台,显示如下内容则说明属性注入成功;
   

2.2 多环境配置

实际开发过程中,常常需要多个环境(如 开发、测试、生产等),而不同环境的配置都不一样,此时配置方法如下;

  1. 创建不同环境对应的配置文件,配置文件名为 application-{profile}.properties{profile} 为我们自定义环境,如下:

application-dev.properties

server.servlet.context-path=/dev

application-test.properties

server.servlet.context-path=/test

application-prod.properties

server.servlet.context-path=/prod
  1. 然后在 application.properties 中加入激活的环境,此时就会激活对应环境的配置;
# {profile} 对应上述的 dev、test、prod
spring.profiles.active={profile}

3. 自定义配置文件

加入我们不想用项目自带的 application.properties 配置环境,那又该如何配置呢?

  1. 首先创建一个自定义配置文件 my.properties,文件名可以自定义,但是后缀要保持一致,然后在其中加入我们自定义配置的属性;
my.id=1024
my.name=村雨遥
my.website=https://cunyu1943.github.io
  1. 定义实体类,用于映射自定义配置文件中的内容;
package com.cunyu.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * @author : cunyu
 * @version : 1.0
 * @className : MyProperties
 * @date : 2020/7/29 14:05
 * @description : TODO
 */

@Component
@PropertySource("classpath:my.properties")
@ConfigurationProperties(prefix = "my")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MyProperties {
    private int id;
    private String name;
    private String website;
}
  1. 定义 Controller 来注入测试
package com.cunyu.controller;

import com.cunyu.pojo.MyProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;

/**
 * @author : cunyu
 * @version : 1.0
 * @className : MyPropertiesController
 * @date : 2020/7/29 14:07
 * @description : TODO
 */

@RestController
@RequestMapping("/my")
public class MyPropertiesController {
    private static final Logger logger = LoggerFactory.getLogger(MyPropertiesController.class);

    @Autowired
    MyProperties myProperties;

    @GetMapping("/profile")
    public String myProfile() {
        logger.info("=============");
        logger.info(myProperties.toString());
        logger.info("=============");

        return myProperties.toString();
    }
}
  1. 打开网页测试,打开 http://localhost:8080/my/profile,同时观察控制台,显示如下内容则说明属性注入成功;
   

4. 注意

  1. application.properties 和 my.applicaiton.properties 会优先加载 application.properties
  2. 本文所有代码均已上传 Github[1],若有需要请自取。

参考资料

[1]

Github: https://github.com/cunyu1943/java-learning-code/tree/master/springboot-learning/spring-boot-01

- END -

更多教程请访问码农之家

往期回顾

通过注解的方式整合 MyBatis + Spring Boot

通过注解的方式整合 Mybatis + PageHelper 分页显示

Spring Boot 通过 XML 的方式整合 MyBatis

标签:Spring,配置,Boot,springframework,application,cunyu,import,org,properties
来源: https://www.cnblogs.com/myhomepages/p/16513809.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有