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