SpringBoot复习:(40)@EnableConofigurationProperties注解的用法

一、配置文件:

server.port=9123

二、配置类:

复制代码
package cn.edu.tju.config;

import com.mysql.fabric.Server;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(ServerProperties.class)
public class MyConfig {

    @Bean("portBean")
    public String getPort(ServerProperties serverProperties){
        return "hello " + serverProperties.getPort();
    }
}

三、测试

复制代码
package cn.edu.tju;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Start {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Start.class, args);
        String result = context.getBean("portBean", String.class);
        System.out.println(result);

    }
}
相关推荐
悟能不能悟2 分钟前
如何处理java.time包类序列化问题,跨版本反序列化 Class对象可能抛出 InvalidClassException
java·开发语言
xxxxxxllllllshi2 分钟前
深入解析单例模式:从原理到实战,掌握Java面试高频考点
java·开发语言·单例模式·面试
一直都在5722 分钟前
Spring:Bean管理(二)
java·sql·spring
Miss_Chenzr2 分钟前
Springboot快递信息管理52c05本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
java·数据库·spring boot
纯粹的热爱3 分钟前
Rust 安装加速指南
后端
千寻技术帮3 分钟前
基于SpringBoot的仿知乎知识问答系统
java·spring boot·毕业设计·论坛·文答
醉卧考场君莫笑4 分钟前
数据分析理论基础
java·数据库·数据分析
廋到被风吹走7 分钟前
【Java】【Jdk】Jdk11->Jdk17
java·开发语言·jvm
南昌彭于晏10 分钟前
解决springboot静态内部类非空校验无效的问题
java·spring boot·后端