starter
1.TcpServer,java
package com.example.startermoduledemo;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class TcpServer {
private String host;
private int port;
public TcpServer(String host, int port) {
this.host = host;
this.port = port;
}
public void start1() {
log.info("tcp server start {}:{}", host, port);
}
public void stop1(){
log.info("stop!");
}
}
2.TcpServerAutoConfiguration.java
package com.example.startermoduledemo;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@EnableConfigurationProperties(TcpServerProperties.class)
@Import(TcpServerLifecycle.class)
public class TcpServerAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public TcpServer getTcpServer(TcpServerProperties properties) {
return new TcpServer(properties.getHost(), properties.getPort());
}
}
3.TcpServerLifecycle.java
package com.example.startermoduledemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.SmartLifecycle;
public class TcpServerLifecycle implements SmartLifecycle {
private volatile boolean running = false;
@Autowired
TcpServer tcpServer;
@Override
public void start() {
running = true;
tcpServer.start1();
}
@Override
public void stop() {
running = false;
tcpServer.stop1();
}
@Override
public boolean isRunning() {
return running;
}
}
4.TcpServerProperties.java
package com.example.startermoduledemo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Data
@ConfigurationProperties(prefix = "tcpserver")
public class TcpServerProperties {
private String host;
private int port;
}
5.META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.startermoduledemo.TcpServerAutoConfiguration
6.pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.7.17
com.example
StarterModuleDemo
0.0.1-SNAPSHOT
StarterModuleDemo
Demo project for Spring Boot
11
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-configuration-processor
true
org.springframework.boot
spring-boot-autoconfigure
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-maven-plugin
paketobuildpacks/builder-jammy-base:latest
org.projectlombok
lombok
使用者
pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.7.17
com.example
StarterModuleDemo
0.0.1-SNAPSHOT
StarterModuleDemo
Demo project for Spring Boot
11
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-configuration-processor
true
org.springframework.boot
spring-boot-autoconfigure
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-maven-plugin
paketobuildpacks/builder-jammy-base:latest
org.projectlombok
lombok
application.properties
tcpserver.host=127.0.0.1
tcpserver.port=6080
入口
package com.example.testimportstarter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.io.IOException;
@SpringBootApplication
public class TestImportStarterApplication {
public static void main(String[] args) {
SpringApplication.run(TestImportStarterApplication.class, args);
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果:
Connected to the target VM, address: '127.0.0.1:3906', transport: 'socket'
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.17)
2023-11-22 00:48:34.261 INFO 7632 --- [ main] c.e.t.TestImportStarterApplication : Starting TestImportStarterApplication using Java 11.0.11 on DESKTOP-JTMBOEI with PID 7632 (D:\2_test_java\TestImportStarter\target\classes started by Administrator in D:\2_test_java\TestImportStarter)
2023-11-22 00:48:34.265 INFO 7632 --- [ main] c.e.t.TestImportStarterApplication : No active profile set, falling back to 1 default profile: "default"
2023-11-22 00:48:34.790 INFO 7632 --- [ main] com.example.startermoduledemo.TcpServer : tcp server start 127.0.0.1:6080
2023-11-22 00:48:34.799 INFO 7632 --- [ main] c.e.t.TestImportStarterApplication : Started TestImportStarterApplication in 1.066 seconds (JVM running for 2.417)
1
2023-11-22 00:48:36.663 INFO 7632 --- [ionShutdownHook] com.example.startermoduledemo.TcpServer : stop!
Disconnected from the target VM, address: '127.0.0.1:3906', transport: 'socket'
感悟:
1.最核心的地方在于: spring.factories提供了我们引入jar时,自动导入下面的bean的方式。
2.XxxAutoConfiguration负责所有这个组件相关Bean的导入即可!
3.使用SmartLifestyle进行组件有顺序的启动