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

    }
}
相关推荐
天天摸鱼的java工程师1 分钟前
CTO新项目直接上MySQL 8.0,老系统仍是5.7
java·后端·mysql
bxlj_jcj3 分钟前
解锁Java多级缓存:性能飞升的秘密武器
java·缓存·面试·架构
未来并未来5 分钟前
Redis 缓存问题及其解决方案
java·redis·缓存
未来影子20 分钟前
SpringAI(GA):Nacos2下的分布式MCP
后端
用户8681812480420 分钟前
【ruoyi-vue-pro】用户相关的设计
后端
快乐肚皮25 分钟前
MySQL集群模式详解:架构、优缺点与生产环境选型指南
java·mysql
季鸢41 分钟前
Java设计模式之备忘录模式详解
java·设计模式·备忘录模式
异常君42 分钟前
Java 逃逸分析:让你的代码性能飙升的秘密
java·面试·代码规范
迢迢星万里灬43 分钟前
Java求职者面试:Spring、Spring Boot、Spring MVC与MyBatis技术深度解析
java·spring boot·spring·面试·mybatis·spring mvc
天天摸鱼的java工程师1 小时前
Nacos 2.0 + 为啥非要三个端口?一次踩坑实录
java·后端