关于spring 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里面的保持一致

关于spring yml文件动态配置的各种注入方式_第1张图片

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层

关于spring yml文件动态配置的各种注入方式_第2张图片

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

你可能感兴趣的:(spring)