Spring Boot学习笔记-1
一、SpringBoot HelloWorld
(1)配置Maven+创建Maven工程
1)在Maven安装目录下的设置文件(settings.xml)中添加镜像
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
2)配置jdk
<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>
3)配置本地仓库
<localRepository>D:\apache-maven-3.6.3\maven-repository</localRepository>
(2)导入SpringBoot依赖(在Maven项目的pom文件中)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
其父项目的父项目为spring-boot-dependencies,是SpringBoot的版本仲裁中心,其所包含的依赖在后续导入时不需要声明版本号
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
spring-boot-starter是SpringBoot场景启动器,导入启动器即导入相关场景全部依赖
(3)编写主程序
1. @SpringBootApplication 标记一个主配置类,说明这是一个SpringBoot应用,SpringBoot就运行这个类的main方法来启动SpringBoot应用
package day1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloWorldApplication {
public static void main(String[] args) {
//启动Spring应用
SpringApplication.run(HelloWorldApplication.class,args);
}
}
2. 编写Controller
package day1.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloWorldController {
//返回内容给浏览器
@ResponseBody
//接收浏览器的hello请求
@RequestMapping("/hello")
public String hello()
{
return "Hello World!";
}
}
(4)部署
SpringBoot不再需要打包成war包放到Tomcat上,可以使用Maven插件直接打包为可执行的jar包
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
打包方式:IDEA右侧Maven中当前项目下的生命周期中有package插件,使用即可打包
使用方式:在jar包所在目录启动命令行,用java -jar命令运行应用
例:D:\Javawork\day1_Demo1\target>java -jar day1_Demo1-1.0-SNAPSHOT.jar
不需要配置Tomcat,因为导入SpringBoot依赖时已经添加相关jar包
二、主配置类
用@SpringBootApplication标记,其内部如下
@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}
)})
1. @SpringBootConfiguration
含义:SpringBoot的配置类
配置类也是容器中的一个组件
2. @EnableAutoConfiguration
含义:开启自动配置功能
//@EnableAutoConfiguration源码
@AutoConfigurationPackage //自动配置包
//该注解的含义为将主配置类所在包及其子包内全部组件扫描到Spring容器
@Import({EnableAutoConfigurationImportSelector.class})
//给容器导入一个组件,由EnableAutoConfigurationImportSelector.class控制
//将所有需导入的组件以类名返回,将其添加到容器(给容器导入若干自动配置类,免去了手写配置注入功能组件这一步骤)