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);

    }
}
相关推荐
韦禾水11 分钟前
记录一次项目部署到tomcat的异常
java·tomcat
曦月合一20 分钟前
树莓派安装jdk、tomcat、vnc、谷歌浏览器开机自启等环境配置
java·tomcat·树莓派
阿丰资源37 分钟前
SpringBoot+Vue实战:打造企业级在线文档管理系统
vue.js·spring boot·后端
Rust研习社1 小时前
使用 Axum 构建高性能异步 Web 服务
开发语言·前端·网络·后端·http·rust
此剑之势丶愈斩愈烈1 小时前
openssl 自建证书
java
面汤放盐1 小时前
何时使用以及何时不应使用微服务:没有银弹
java·运维·云计算
0xDevNull1 小时前
Spring Boot 自动装配:从原理到实践
java·spring boot·后端
IT_陈寒1 小时前
SpringBoot配置加载顺序把我坑惨了
前端·人工智能·后端
Moment2 小时前
面试官:给 llm 传递上下文,有哪几个身份 role ❓❓❓
前端·后端·面试
qq_589568102 小时前
java学习笔记,包括idea快捷键
java·ide·intellij-idea