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

    }
}
相关推荐
考虑考虑39 分钟前
zookeeper认证
后端·zookeeper
掘金者阿豪1 小时前
数据库选型到底应该看什么?从业务需求到技术架构的实践分析
前端·后端
codigger1 小时前
ThreadLocal 会内存泄露吗?
java·内存泄漏·threadlocal
挖掘狂人1 小时前
ThreadLocal 会内存泄露吗?
java·安全·架构
她的男孩2 小时前
审批人选了「部门主管」,发起却报 Unknown property:Forge Admin 启动时补齐组织变量
spring boot·低代码·工作流引擎
苍何2 小时前
豆包终于想通了...
后端
AAA修煤气灶刘哥2 小时前
Agent 都能干活了,软件还需要那么多按钮吗?
人工智能·后端·架构
未秃头的程序猿2 小时前
给新模块加了个 @MapperScan,启动崩了,最后发现跟 MapperScan 没关系
java·后端·mybatis
苍何2 小时前
不用 Opus5.5 ,即梦也能出爆款 vibe 视频了
后端
SimonKing2 小时前
WorkBuddy又推出新的限免模型(Hy4 flash)了,羊毛可以薅起来了
java·后端·程序员