首页 > 其他分享 >SpringBoot复习:(40)@EnableConofigurationProperties注解的用法

SpringBoot复习:(40)@EnableConofigurationProperties注解的用法

时间:2023-08-21 17:38:13浏览次数:48  
标签:EnableConofigurationProperties SpringBoot springframework 40 context import org 


一、配置文件:
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);

    }
}


标签:EnableConofigurationProperties,SpringBoot,springframework,40,context,import,org,
From: https://blog.51cto.com/amadeusliu/7177747

相关文章