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

    }
}
相关推荐
ElevenPlus5 分钟前
【 AI智能体时代:一名Javaer的技术随想录】MCP服务部署架构
后端
下次再写11 分钟前
Java互联网大厂面试技术问答实战:涵盖Java SE、Spring Boot、微服务及多场景应用
java·数据库·缓存·面试·springboot·microservices·技术问答
公众号-老炮说Java13 分钟前
IDEA 2026.1 + Claude Code = 降维打击
java·ide·intellij-idea
千寻girling19 分钟前
RabbitMQ 详细教程(38K字数)
java·后端·面试
止语Lab27 分钟前
Go vs Java GC:同一场延迟战争的两条路
java·开发语言·golang
Rust研习社27 分钟前
Rust 多线程从入门到实战
开发语言·后端·rust
卷毛的技术笔记34 分钟前
从“拆东墙补西墙”到“最终一致”:分布式事务在Spring Boot/Cloud中的破局之道
java·spring boot·分布式·后端·spring cloud·面试·rocketmq
ERBU DISH40 分钟前
修改表字段属性,SQL总结
java·数据库·sql
袋鱼不重1 小时前
Hermes Agent 直连飞书机器人
前端·后端·ai编程
云烟成雨TD1 小时前
Spring AI Alibaba 1.x 系列【26】Skills 生命周期深度解析
java·人工智能·spring