mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
spring boot 自动化配置示例
This commit is contained in:
@@ -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>
|
||||
|
||||
3
lab-47/lab-47-demo/src/main/resources/application.yaml
Normal file
3
lab-47/lab-47-demo/src/main/resources/application.yaml
Normal file
@@ -0,0 +1,3 @@
|
||||
yunai:
|
||||
server:
|
||||
port: 8888
|
||||
@@ -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>
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user