spring boot 自动化配置示例

This commit is contained in:
YunaiV
2020-02-02 12:56:55 +08:00
parent e0c63ab31f
commit cea438c986
4 changed files with 9 additions and 4 deletions

View File

@@ -12,6 +12,7 @@
<artifactId>lab-47-demo</artifactId>
<dependencies>
<!-- 引入自定义 Starter -->
<dependency>
<groupId>cn.iocoder.springboot.labs</groupId>
<artifactId>yunai-server-spring-boot-starter</artifactId>

View File

@@ -0,0 +1,3 @@
yunai:
server:
port: 8888

View File

@@ -12,6 +12,7 @@
<artifactId>yunai-server-spring-boot-starter</artifactId>
<dependencies>
<!-- 引入 Spring Boot Starter 基础库 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>

View File

@@ -11,14 +11,14 @@ import org.springframework.context.annotation.Configuration;
import java.io.IOException;
import java.net.InetSocketAddress;
@Configuration
@EnableConfigurationProperties(YunaiServerProperties.class)
@Configuration // 声明配置类
@EnableConfigurationProperties(YunaiServerProperties.class) // 使 YunaiServerProperties 配置属性类生效
public class YunaiServerAutoConfiguration {
private Logger logger = LoggerFactory.getLogger(getClass());
@Bean
@ConditionalOnClass(HttpServer.class)
@Bean // 声明创建 Bean
@ConditionalOnClass(HttpServer.class) // 需要项目中存在 com.sun.net.httpserver.HttpServer 类。该类为 JDK 自带,所以一定成立。
public HttpServer httpServer(YunaiServerProperties serverProperties) throws IOException {
// 创建 HttpServer 对象,并启动
HttpServer server = HttpServer.create(new InetSocketAddress(serverProperties.getPort()), 0);