关于spring yml文件动态配置的各种注入方式

本文探讨了Spring中使用YML文件进行动态配置的两种主要方式:@Value注解和@ConfigurationProperties注解。通过YML配置文件,可以方便地管理如FTP服务器的配置信息,避免代码硬编码,便于后期维护。示例展示了如何正确匹配YML配置的层级和名称,以确保属性成功注入。

       spring容器支持多种注入方式,在工作中运用的场景也特别的多,我们需要清楚一点,spring的本质就是容器,通过spring可以很好的实现类的调用方和被调用方直接的解耦,同时把类的创建和销毁交接给spring进行管理,不过我们也会经常遇到一些需求变更,比如你需要利用ftp上传文件,文件路径暂时不明确,如果把文件路径,用户名,端口这些配置信息,写死在代码里面也不合适,如果有变更,只能通过改代码和发版本来解决,所以放置在yml文件上是一个很不错的选择,如果需要变更配置,直接在外部的yml上面修改信息,然后重启jar包即可

      下面总结一下常用的一些方法

       首先我们需要在yml文件里面配置一些响应的properties信息

ftpConfig:
  host: 192.168.1.171
  username: root
  password: 123
  path: /opt/temp

1.@Value注解

      在Controller层直接使用@Value方法,观察一下属性是否成功注入

        

package com.mimoprint.schedule.controller;

import com.mimoprint.schedule.model.FtpConfig;
import com.mimoprint.schedule.model.RestResult;
import org.springframework.beans.BeanUtils;
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
@RequestMapping("/api")
public class MyController {

    @Value("${ftpConfig.host}")
    private String host;

    @Value("${ftpConfig.username}")
    private String username;

    @Value("${ftpConfig.password}")
    private String password;

    @Value("${ftpConfig.path}")
    private String path;


    @GetMapping("/getFtpMessage")
    public RestResult getFtpMessage() {
        FtpConfig config = new FtpConfig(host, username, password, path);
        return new RestResult(200, "success", path);
    }
}

  通过断点可以观察到,属性已经通过@Value注入到Controller层。

  注意:@Value里面的value值的层级关系和名称必须和yml里面的保持一致

2.@ConfigurationProperties注解:

把属性都封装到一个bean里面,通过@Component注解交给spring管理,代码如下

package com.mimoprint.schedule.model;

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

@Component
@ConfigurationProperties(prefix = "ftp-config")
public class FtpConfig {

    private String host;
    private String username;
    private String password;
    private String path;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }
}

       这里面有一点需要注意一下,就是prefix就是yml的配置信息的头部,之前写成小驼峰ftpConfig在@value注解使用没有问题,但是在这里prefix也用小驼峰是会报错的,会出现下面这个信息,所以我把他修改了一下名称。

Configuration property name 'ftpConfig' is not valid:

    Invalid characters: 'C'
    Bean: myController
    Reason: Canonical names should be kebab-case ('-' separated), lowercase alpha-numeric characters and must start with a letter

Action:
package com.mimoprint.schedule.controller;

import com.mimoprint.schedule.model.FtpConfig;
import com.mimoprint.schedule.model.RestResult;
import org.springframework.beans.BeanUtils;
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
@RequestMapping("/api")
public class MyController {

    @Autowired
    private FtpConfig ftpConfig;

    @GetMapping("/getFtpMessage")
    public RestResult getFtpMessage() {
        return new RestResult(200, "success", ftpConfig);
    }
}

通过断点我们一样可以看到属性已经注入到Controller层

以上是对yml注入的两种解决方案,因为工作中用到了,在这里mark一下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值