美文网首页
注解记录->ConfigurationProperties

注解记录->ConfigurationProperties

作者: 云鲸鱼rain | 来源:发表于2019-02-14 17:32 被阅读0次

本来这个简单的注解觉得不用单开文章写了,自己测试了一下,就发现了bug,还是写一下吧。

先贴正确代码

package com.tianci.config;

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

/**
 * Create by tianci
 * 2019/2/14 17:10
 */

@Data
@ConfigurationProperties(prefix = "mysqlconfig")
@Component
public class MysqlConfig {

    private String url;

    private String driver;

    private String username;

    private String password;
}
mysqlconfig:
  username: root
  password: root
  url: jdbc:mysql://localhost:3306/test
  driver: com.mysql.jdbc.Driver
package com.tianci.controller;

import com.tianci.config.MysqlConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Create by tianci
 * 2019/2/14 17:16
 */

@RestController
public class MysqlController {

    @Autowired
    private MysqlConfig mysqlConfig;

    @GetMapping("/getMysqlConfig")
    public void getMysqlConfig() {
        System.out.println(mysqlConfig.getUrl());
        System.out.println(mysqlConfig.getDriver());
        System.out.println(mysqlConfig.getUsername());
        System.out.println(mysqlConfig.getPassword());
    }
}
//运行结果
jdbc:mysql://localhost:3306/test
com.mysql.jdbc.Driver
root
root

异常内容

Description:

Configuration property name 'MysqlConfig' is not valid:

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

Action:

Modify 'MysqlConfig' so that it conforms to the canonical names requirements.

原因是命名不规范,不要大写字母,使用小写字母,可加中划线-。

相关文章

网友评论

      本文标题:注解记录->ConfigurationProperties

      本文链接:https://www.haomeiwen.com/subject/mwfyeqtx.html