一、前言
Spring 是 Java 開發(fā)非常流行且優(yōu)秀的框架榜聂,一般用來做 Web 開發(fā)宁脊,但是如果我們只想使用 Spring 提供的容器環(huán)境和方便的工具組件箍镜,不想啟用 Tomcat 、數(shù)據(jù)庫等繁雜的組件該如何做渔欢?
二钧惧、去掉數(shù)據(jù)庫
只需要在啟動(dòng)類上加上一個(gè)屬性,去掉數(shù)據(jù)源的自動(dòng)裝配即可
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableScheduling
public class App implements ApplicationContextAware {
private static final Logger log = LoggerFactory.getLogger(App.class);
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
三坐漏、去掉Servlet容器
最簡單的方式是直接不引入 spring-boot-starter-web
這個(gè)包,但是有時(shí)候我們又想使用 RestTemplate
等好用的工具酷誓,可以這樣配置單獨(dú)去掉 Tomcat
。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 去除內(nèi)嵌tomcat -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
四态坦、后記
去掉不用的功能后啟動(dòng)時(shí)間極具縮短盐数,同時(shí)還能享受 Spring 的福利,美滋滋伞梯。
2020-10-23 17:28:37.923 INFO 260 --- [ main] com.juyuansoft.buildcloud.iot14.App : Starting App on DESKTOP-U21EBB6 with PID 260 (C:\jy\buildcloud-iots\buildcloud-iot-14\target\classes started by zhanghuan in C:\jy\buildcloud-iots\buildcloud-iot-14)
2020-10-23 17:28:37.930 INFO 260 --- [ main] com.juyuansoft.buildcloud.iot14.App : No active profile set, falling back to default profiles: default
2020-10-23 17:28:39.409 INFO 260 --- [ main] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService 'taskScheduler'
2020-10-23 17:28:39.450 INFO 260 --- [ main] com.juyuansoft.buildcloud.iot14.App : Started App in 1.987 seconds (JVM running for 2.702)
注意玫氢,去掉這些功能后,項(xiàng)目如何常駐就需要我們自己編寫了谜诫。