springboot情操陶冶-初识springboot

时间:2024-06-10 11:06:08

前言:springboot由于其轻便和去配置化等的特性已经被广泛应用,基于时代潮流以及不被鄙视,笔者于是开辟此篇开始认识springboot

前话

springboot是基于spring而开发的轻量级框架,所以在学习springboot之前,务必对spring的工作模式和源码有一定的了解。笔者此处就不展开了,如果有兴趣可直接戳以下链接阅读即可

Spring源码情操陶冶-ContextLoaderListener

springboot框架概览

springboot情操陶冶-初识springboot

具体更多的信息,可参考spring官网#springboot。笔者此处对上述的配图作下简单的翻译

springboot是什么

  1. springboot让用户可以更为简单的去创建独立的、基于spring的应用程序并且运行简单化。

  2. springboot固执的认为通过spring平台和其他的第三方包就可以轻松的运行相应的程序。并且说明大部分的springboot应用只需要少部分的spring配置,也就是去配置化

springboot特点

  • 创建独立的Spring应用程序

  • 直接内置Tomcat/Jetty/UnderTow等web容器(去war方式部署运行)

  • 提供starter依赖以简化用户的构建配置

  • 当需要的时候自动配置Spring和第三方依赖包

  • 提供准生产特征,比如metrics(度量)、heath checks(健康检查)、externalized configuration(外部化配置)

  • 无代码生成和无XML配置要求

springboot用法

笔者此处不分析springboot的相关用法,相关内容在官方的文档上已经提的非常清楚,有兴趣的直接戳链接前往阅读。Spring Boot Reference

普通方式运行

jdk(1.8)/springboot(2.0.3.RELEASE)/spring(5.0.7.RELEASE)


maven配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>demo-springboot</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

启动类

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class DemoSpringbootApplication { public static void main(String[] args) {
SpringApplication.run(DemoSpringbootApplication.class, args);
}
}

web方式运行

jdk(1.8)/springboot(2.0.3.RELEASE)/spring(5.0.7.RELEASE)


maven配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId>
<artifactId>demo-springboot-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>demo-springboot-web</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

启动类

package com.example.demospringbootweb;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class DemoSpringbootWebApplication { public static void main(String[] args) {
SpringApplication.run(DemoSpringbootWebApplication.class, args);
}
}

小结

笔者以此篇作为springboot的开篇,后续便对springboot的源码作下简单的分析,方便读者和笔者知其所以然而了如其然