一、Spring Boot 入门
1、Spring Boot简介
简化Spring应用开发的一个框架;
整个Spring技术的一个大整合;
J2EE开发的一站式解决方案;
特性
1.快速创建独立运行的Spring项目以及与主流框架集成
2.使用嵌入式的Servlet容器,应用无需打成war包,可打成jar包直接运行
3.starters自动依赖与版本控制
4.大量的自动配置,简化开发,也可修改默认值
5. 无需配置XML,无代码生成,开行即用
6. 准生产环境的运行时应用监控
2、微服务
2014,Martin Fowler
微服务:架构风格
一个应用应该是一组小型服务;可以通过http的方式进行互通。
每一个功能元素最终都是一个可独立替换和 独立升级的软件单元;
3、 环境准备
- jdk1.8+
- maven3.x+
- intellijIDEA2.17+
- springboot1.5.9RELEASE
统一环境:
3.1MAVEN 设置
全局jdk配置,settings.xml
<profile>
<id>jdk18</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
3.2IDEA设置
4、SpringBoot HelloWorld
4.1、intellij idea创建一个Spring Boot的maven工程
选择web应用
注意
- Finish之后等待idea加载jar包,构建项目
- 项目中的.mvn包,mvnw与mvnw.cmd文件可以删除
补充: - resources文件夹目录结构
- static:保存所有的静态资源;js css images
- templates:保存所有的模板页面;(SpringBoot默认jar包使用嵌入式Tomcat,默认不支持JSP页面)可以使用模板引擎(freemarker thymeleaf)
- application.properties:SpringBoot应用的配置文件;可以修改默认配置
例如修改端口号:
4.2、使用maven管理,生成的pom文件内容
<?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 https://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.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot01</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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4.3、编写一个主程序;启动Spring Boot应用
/*
* @SpringBootApplication 来标注一个主程序类,说明这是一个spring boot应用
*/
@SpringBootApplication
public class HelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class,args);
}
}
4.4、编写相关的Controller、Service
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "Hello World";
}
}
4.5、 启动主程序,访问项目
4.6、简化部署
4.6.1利用maven插件打成jar包
maven插件,可以将应用导包成一个可执行的jar包
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
- 在MavenProjects运行package命令
打包完成
4.6.2用java -jar命令运行
复制jar包,在其目录下运行java -jar springboot01-0.0.1-SNAPSHOT.jar
注意:打包时,如果是自己手动写的主程序类,比如HelloWorldApplication;需要把自动生成的主程序类和test下相关信息全部删除,否则build fail
5、Hello World探究
5.1、POM文件
5.1.1、父项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
它的父项目是
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
它才是真正管理Spring Boot应用里面的所有依赖版本
Spring Boot的版本仲裁中心:
以后导入依赖默认是不需要写版本;(没有在dependencies里面管理的依赖需要声明版本号)
5.1.2、导入的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring-boot-starter-web :
spring-boot-starter:spring-boot场景启动器;帮我们导入了web模块正常运行所依赖的组件;
参考文章 查看其它组件引入pom
Spring Boot将所有的功能场景都抽取出来,做成一个个的starters(启动器),只需要在项目里面引入这些starter,相关场景的依赖会导入进来,要用什么功能就导入什么场景的启动器。
5.2、主程序类、主入口类
/*
* @SpringBootApplication 来标注一个主程序类,说明这是一个spring boot应用
*/
@SpringBootApplication
public class HelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class,args);
}
}
@SpringBootApplication:spring boot应用标注在某个类上,这个类就是SpringBoot的主配置类,SpringBoot应用这个类的main方法启动应用。
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
@SpringBootConfiguration:SpringBoot的配置类:
标注在某类上,该类就是SpringBoot的配置类:
@Configuration:配置类上标注这个注解:
配置类 ------配置文件;配置类也是容器中的一个组件;@Component
@EnableAutoConfiguration:开启自动配置功能;
以前我们需要配置的文件,SpringBoot帮我们自动配置。
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
@AutoConfigurationPackage:自动配置包
@Import({Registrar.class}):Spring的底层注解@import,给容器中导入一个组件;导入组件由Registrar.class;
将主配置类的所在包及下面所有子包里面的所有组件扫描到Spring容器;
@Import({AutoConfigurationImportSelector.class}):
给容器导入组件 ?
AutoConfigurationImportSelector:导入组件的选择器;
将所有需要导入的组件以全类名的方式返回;这些组件会被添加到容器中;
会给容器中导入非常多的自动配置类(xxxAutoConfiguration);给容器导入这个场景中所需要的所有组件,并配置好这些组件。
有了自动配置类,免去手动编写配置注入功能组件等工作;
SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
SpringBoot在启动的时候,从类路径下的\META-INF\spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为自动配置类导入到容器中,自动配置类生效,进行自动配置工作。
J2EE的整体整合解决方案和自动配置都在spring-boot-autoconfigure-2.2.2.RELEASE.jar
学习记录成长点滴,不喜勿喷谢谢|ू・ω・` )