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

    }
}
相关推荐
AINative软件工程2 分钟前
LLM 上下文满了别直接报错:Context Eviction 工程实践,5 种淘汰策略的生产对比
后端·llm·ai编程
Wang's Blog4 分钟前
Java框架 SpringCloud 快速入门: 微服务框架课程介绍与知识地图
java·spring cloud·微服务
Cc.Y11 分钟前
Java零基础入门:集合框架:ArrayList 与 LinkedList —— 告别数组的“死板“,拥抱动态容器的“灵活
java·开发语言·python
FYKJ_201021 分钟前
springboot游泳馆管理系统79222-计算机课程设计、毕业设计
vue.js·spring boot·后端·python·mysql·django·课程设计
ドロロ80627 分钟前
openAPI与Restful API的区别
后端·restful
再吃一根胡萝卜33 分钟前
大模型多模态、Harness 与 MCP:一个 Agent 开发者的技术笔记
后端
现任明教教主~2 小时前
SpringBoot+Vue学生宿舍管理系统源码 带部署文档
vue.js·spring boot·后端
panxianren8 小时前
Bun 1.4 的 bun test --parallel 将 4 秒的测试套件缩短到约 1 秒
java·javascript
码事漫谈9 小时前
骂AI,会让它做得更好吗?
后端
sunshine22 girl9 小时前
Java学习七 Java 项目实战2-注册业务
java·学习