SpringBoot介绍
Spring Boot对Spring平台和第三方库进行了整合,可创建可以运行的、独立的、生产级的基于Spring的应用程序。(大多数Spring Boot应用程序只需要很少的Spring配置)
Spring Boot可以使用java -jar或更传统的war部署启动的Java应用程序进行创建,可以内嵌Tomcat 、Jetty .Undertow容器,快速启动web程序。
设计目标
- 为所有Spring开发提供更快且可通用的入门体验
- 开箱即用,可以根据需求快速调整默认值。
- 提供大型项目(例如嵌入式服务器、运行状况检查和统一配置)通用的一系列非功能性功能
- 绝对没有代码生成,也不需要XML配置。
SpringBoot 优势对比预览
1. SpringMVC方式
(1) 修改pom.xml成类似以下的内容
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.study</groupId>
<artifactId>spring-mvc</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>spring-mvc Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>spring-mvc</finalName>
</build>
</project>
(2) 创建并修改web.xml成类似以下的内容
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!-- 编码过滤 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- springMVC DispatcherServlet 配置 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 指定配置文件 -->
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
(3) 创建并修改dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 配置静态资源由web服务器的默认servlet处理 -->
<mvc:default-servlet-handler />
<!-- 启动注解支持 -->
<mvc:annotation-driven/>
<!--
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true"> -->
<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
<!-- <bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<mvc:default-servlet-handler/>
-->
<!-- 配置扫描注解组件的基础包 -->
<context:component-scan base-package="com.**"/>
<!-- 配置视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
</bean>
<!-- 国际化配置 -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages"/>
<!-- <property name="basenames">
<array>
<value></value>
</array>
</property> -->
</bean>
<!-- 异常映射配置 -->
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="/error/error"/>
<property name="defaultStatusCode" value="500"/>
<property name="exceptionMappings">
<props>
<prop key="java.sql.SQLException">/error/500</prop>
</props>
</property>
<property name="warnLogCategory" value="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"/>
</bean>
</beans>
(4) 创建Controller
package cn.lazyfennec.mvc.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@GetMapping("/index")
public String index() {
return "hello world";
}
}
(5) 部署项目到tomcat
(6) 访问指定地址进行测试
2. SpringBoot方式
(1) 查看pom.xml
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.8.BUILD-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.lazyfennec</groupId>
<artifactId>springBootDemo</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>springBootDemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
... 省略部分内容
(2) 创建controller
这里可以类似上方的mvc模式
(3) 运行并且进行测试
可以看到上方的两种方式的比较,SpringBoot的方式比较简单,非常快速,避免了很多的配置,并且无需配置tomcat相关的内容。
Maven工程使用SpringBoot
(1) pom.xml中加入以下内容
- dependencyManagement
<dependencyManagement>
<dependencies>
<dependency>
<!--从SpringBoot导入依赖关系管理-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
- dependencies
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
(2) 创建启动类
package cn.lazyfennec;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication//由本注解指定启动类
public class SpringBootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
}
}
(3) 创建Controller
package cn.lazyfennec.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@GetMapping("/index")
public String index() {
return "hello world";
}
}
运行及打包
1.通过IDEA运行main方法
- maven插件运行: mvn spring-boot:run,需要添加spring-boot-maven-plugin到我们的pom.xml文件中
3.创建可执行的jar,需要添加spring-boot-maven-plugin到我们的pom.xml文件中
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
- 打包命令: mvn package
- 执行命令: java -jar xxx.jar
- 注意事项: jar文件生成在target目录下,**..jar.original这个文件一般很小,这是打包可执行jar文件之前的原始jar
一些需要注意的东西
- 程序入口: Main方法
@SpringBootApplication//由本注解指定启动类
public class SpringBootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
}
}
- SpringBoot中通用的约定
- 注解扫码的包目录basePackage为启动类Main函数入口所在的包路
- 配置文件约定是classpath目录下的application.yml或者application.properties
- web开发的静态文件放在classpath,访问的顺序依次是:
/META-INF/resources -> resources -> static -> public - web开发中页面模板,约定放在classpath目录,/templates/目录下
如果觉得有收获就点个赞吧,更多知识,请点击关注查看我的主页信息哦~