一、Spring Boot
1、简介
**特点**:微服务 架构风格、可以通过Http协议沟通
每一个功能元素最终都是一个功能单元
部署和运维压力大
MAVEN设置
1.8
setting.xml配置
<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>
2、spring Boot HelloWorld
一个功能
浏览器发送hello请求,服务器接收请求并处理,响应Hello World
1、创建一个工程 maven
2、导入依赖spring boot 相关
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
3、编写一个主程序 启动spring Boot应用
/**
- Create by kai on 2020/4/8 16:47
*/
@SpringBootApplication
public class HelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class,args);
}
}
4、编写相关controller、Service
@Controller
public class HelloController {
@ResponseBody
@RequestMapping
public String hello(){
return "Hello World";
}
}
5、运行测试主程序
6、简化部署
<!-- 打包一个可执行文件jar包-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
将这个应用直接使用java-jar的命令进行执行
3、Hello World探究
1、POM文件
1、父项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--依赖的父项目 管理版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
2、启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
**spring-boot-starter**:spring-boot场景启动器,帮组我们导入web模块正常运行所依赖的组件;
spring-boot 将所有功能场景都抽取出来,做成一个个的starters(启动器),只需要在项目里面引用starter相关场景的所有依赖都会导入进来。
2、主程序类,主入口类
@SpringBootApplication
public class HelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class,args);
}
}
@SpringBootApplication :spring boot 应用标注在某个类,说明是主配置类。运行这个类,启动spring boot;
@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:配置类上来标志这个注解
配置类--配置文件 也是容器中的一个组件 @component
@EnableAutoConfiguration :开启自动配置
以前我们需要配置的东西,自动帮我们配置了
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
@AutoConfigurationPackage :自动配置包
@Import({Registrar.class})
Spring 的底层注解 给容器导入一个组件,导入的组件由
AutoConfigurationPackage.Registrar.class
将主配置类(@SpringBootApplication标志的类) 的所在包及下组件扫描到spring容器中
@Import({AutoConfigurationImportSelector.class})
给容器中导入组件
AutoConfigurationImportSelector.class 导入哪些组件的选择器
将所有需要导入的组件以全类名的方式返回;这些组件就会被添加到容器中;
会给容器中导入非常多的自动配置类(xxxAutoConfiguration)就是给容器中导入这个场景需要的所有组件,并配置好这些组件。
有了自动配置类,免去手动配置
Spring Boot在启动的时候从类路径下的META-INF/Spring.factories中获取AutoConfiguration指定的值,将这些值作为自动配置类导入到容器中,生效自动配置。
J2EE的整体整体解决方案
spring-boot-autoconfigure-2.2.6.RELEASE.jar;
4、使用Spring Initializer快捷创建Spring Boot项目
**IDEA都支持此功能;**
选择我们需要的模块,联网创建项目
默认生成的SpringBoot项目:主程序已生成,只需要逻辑添加
resources文件中
static 静态资源:js,css,images
templates:保存所有的模板页面:spring boot 默认jar包使用嵌入式的Tomcat,默认不支持jsp页面;可以使用模板引擎(freemarker、thymel)
application.properties:应用的配置文件;可以修改以前的默认设置