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

    }
}
相关推荐
liangbo71 分钟前
Spring系列01:IoC 与 DI 到底解决了什么
java·spring
江湖中的阿龙5 分钟前
Oracle 紧急安全警报:Java 9.1 分漏洞 CVE-2026-91403,撤销对 eMudhra 风险证书的信任
java·安全·oracle
明月_清风8 分钟前
为什么 Skill 很难商业化?Bot 才是 AI 时代真正的商业机会
人工智能·后端·agent
苍何11 分钟前
DeepSeek 的 Super-agent 设计,和我们的工程实践
后端
挖掘狂人11 分钟前
缓存和数据库的一致性,别停在 “延时双删”
java·后端·程序员
codigger18 分钟前
缓存和数据库的一致性,别停在 “延时双删”
java·后端·程序员·后端开发
10年前端老司机18 分钟前
Agent 商业化困局:热闹的发布会,冷清的付费客户
前端·后端·agent
Java后端的Ai之路22 分钟前
Java并发编程07-等待通知机制优化循环等待
java·数据库·python
小小张说故事23 分钟前
Python 的 async def 为什么能同时跑几千个任务?拆开协程对象、await 与事件循环那次让出
后端·python
Raas10028 分钟前
AI网关如何接入?MAI Gateway部署实操教程
java·开发语言·人工智能·gateway·ai网关·mai gateway·企业级产品