SpringBoot 官方文档示例:

SpringBoot 官方文档示例:,第1张

一、配置类,不能加@Configuration注解,否则报错

package cn.edu.tju.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;
import org.springframework.context.annotation.Configuration;

@ConfigurationProperties
@ConstructorBinding
public class AppConfig2 {
    private  String fristName;
    private  String lastName;

    public AppConfig2(String firstName,String lastName){
        this.fristName=firstName;
        this.lastName=lastName;
    }

    public String getFristName() {
        return fristName;
    }

    public void setFristName(String fristName) {
        this.fristName = fristName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

二、启动类加@ConfigurationPropertiesScan注解或
@EnableConfigurationProperties(AppConfig2.class) 注解

package cn.edu.tju;

import cn.edu.tju.config.AppConfig2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@SpringBootApplication
//@EnableConfigurationProperties(AppConfig2.class)
@ConfigurationPropertiesScan

public class Start {
    public static void main(String[] args) {
        //SpringApplication.run(Start.class,args);

        SpringApplication springApplication=new SpringApplication(Start.class);
        //springApplication.setEnvironmentPrefix("amadeus");
        springApplication.run(args);

    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/789082.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-05
下一篇 2022-05-05

发表评论

登录后才能评论

评论列表(0条)

保存