spring boot简介
spring boot来简化spring应用开发,约定大于配置、去繁从简,just run就能创建一个独立的,产品级别的应用
背景:
j2ee笨重的开发、繁重的配置、底下的开发效率、复杂的部署流程、第三方技术集成难度大。
解决:
spring全家桶时代
springboot--> j2ee一站式解决方案
spring cloud-->分布式整体解决方案
springboot的优点
1、快速创建独立运行的spring项目以及与主流框架集成
2、使用嵌入式的Servlet容器应用无需打成war包
3、starters自动依赖与版本控制
4、大量的自动配置,简化开发,也可以修改默认值
5、无需配置xml,无需代码生成。开箱即用
6、准生产环境的运行时应用控制
7、与云计算的天然集成
缺点:
1、入门容易精通难
2、从原来的xml配置方式转换到JAVA配置方式变化有点大
3、版本迭代速度很快,一些模块改动很大
5、由于不用自己做配置,报错时很难定位
6、网上现成的解决方案比较少
什么是微服务?
微服务是一种架构风格,一个应用应该是一组小型服务;可以通过http的方式进行沟通。
单体应用:all in one
maven设置
<profiles>
<profile>
<id>jdk-1.8</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>
</profiles>
springboot 的helloworl搭建
先建一个maven SpringBoot项目作为一个父项目
在建一个Model
导入boot相关的依赖
<?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">
<parent>
<artifactId>SpringBoot</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>SpringBoot-01</artifactId>
<description>spring boot hello world</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.4.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<!--把应用程序打成可运行的jar包-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
编写一个主程序
package com.yu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @SpringBootApplication 用来标注一个启动类
*/
@SpringBootApplication
public class ApplicationHelloWorld {
public static void main(String[] args) {
//启动应用程序
SpringApplication.run(ApplicationHelloWorld.class,args);
}
}
启动结果
编写一个controller类
package com.yu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@ResponseBody //把字符串响应到页面
@RequestMapping("/hello")
public String hello(){
return "hello";
}
}
响应结果
打成jar包
成功后可以看到jar包的目录
java -jar SpringBoot-01-1.0-SNAPSHOT.jar
这里可以会出现一个问题
SpringBoot-01-1.0-SNAPSHOT.jar中没有主清单属性
意思是说需要没有导入maven打包的依赖或者是一个导的依赖不对
这时可以试试下面这个依赖
导入后 先执行 install
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
探究一下HelloWorld
1、pom文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
这是要给父目录
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.4.3</version>
</parent>
他是真正管理spring boot应用里面的所有依赖版本;
有了它以后就不用写版本号了
2、依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring-boot场景启动器,帮我们导入了web模块正常运行所依赖的组件;
将所有的功能场景都抽取出来,做成一个个的starters(启动器),只要在相关的场景导入这些启动器依赖就导入进来,要用什么功能就导入什么样的场景启动器
3、主程序类
/**
* @SpringBootApplication 用来标注一个启动类
*/
@SpringBootApplication
public class ApplicationHelloWorld {
public static void main(String[] args) {
//启动应用程序
SpringApplication.run(ApplicationHelloWorld.class,args);
}
}
@SpringBootApplication: sprng boot应用标注在某个类上说明这个类是Spring Boot的主配置类,Spring Boot就应还运行这个类的main方法来启动SpringBoot应用;
@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:spring boot的配置类
标注在某个类上,表示这个类是一个Spring Boot的配置类;
@Configuration:在类上配置这个注解来标注它是一个配置类,这个配置类---和在spring中的配置文件一样;
配置类也是容器中的一个组件;@Component
@EnableAutoConfiguration:开启自动配置功能;
以前需要配置的东西,Spring Boot帮我们自动配置;@EnableAutoConfiguration告诉Spring Boot开启自动配置功能;这样自动配置才能生效;
@AutoConfigurationPackage //自动配置配置包
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
@AutoConfigurationPackage这个自动配包是怎么实现?
@Import({Registrar.class}) 这是一个spring的注解,给容器导入一个组件Registrar
@Import({AutoConfigurationImportSelector.class})
将主配置类(@SpringBootApplication标注的类)的所有包及下面的所有子包里面的所有组件扫描打扫spring容器;
@Import({AutoConfigurationImportSelector.class}):给容器中导入组件?
AutoConfigurationImportSelector:导入那些组件的选择器;配置类的名称为:XXXutoConfiguration
springboot 在启动的时候从类路径先的META-INF/spring.factories中获得相应的值,将这些值作为自动配置类导入到容器中,自动配置类就生效。
自动配置原理
1、spring boot启动会加载大量的自动配置类
2、看需要的功能有没有spring boot默认写好的自动配置类;
3、在来看这个自动配置类中到底配置了那些组件;
4、给容器中自动配置添加组件的时候,会从properties类中获取某些属性。我们就可以在配置文件中指定这些属性的值;
xxxAutoConfiguration:自动配置类;
给容器中添加组件
xxxProperties:封装配置文件中相关属性;
可用通过debug=true来查看那些类是生效的(Positive matches),那些是不生效的(Negative matches)