一、所需掌握的基础。
Spring框架使用经验。
MAVEN使用。
Eclipse或IDEA。
二、环境
JDK1.8++
maven3.3++
intellij idea 2017
spring boot 1.5.9
1.maven settings.xml配置
<profiles>
<profile>
<id>jdk-1.8</id>
<activation>
<activateByDefault>true</activateByDefault>
<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>
2.IDEA设置
二、HelloWorld
1.new project。
选择spring initializr
输入group、artifact、package等相关信息
选择需要的模块
点击finish
更改目录属性。
在src->main->java上右键->mark directory as->Sources Root
在src->main->resources上右键->mark directory as->Resources Root
2.新建controller
点击helloword,新建java class
输入包名.类名。
完善内容,增加controller注解,添加hello方法。
package com.roger.helloworld.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
* @RestController 等同于
* @ResponseBody
* @Controller
* 表示这个类的所有方法返回数据直接写给浏览器,如果是对象转换为json
*/
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "hello world";
}
}
3.运行程序
在SpringBoot01HelloworldApplication类中右键运行,显示运行成功,端口是8080。
浏览器输入地址:http://127.0.0.1:8080/hello,正常返回数据:
4.部署运行
在pom.xml已经自动引入了打包的插件。
<!-- 这个插件可以将应用打包成一个可执行的jar包-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
执行打包操作。
在target目录中生成了打包好后的jar包。
在命令行中进入target目录,执行java -jar spring-boot-01-helloworld-0.0.1-SNAPSHOT.jar 运行程序。
5.文件分析
目录结构:
pom.xml:maven配置文件。
src/main/java:java程序文件目录。
src/resources:静态资源文件,配置文件。
src/test:测试程序文件目录
target:项目打包后生成的文件。
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
他的父项目,用来管理所有spring boot项目的所有依赖,依赖中已经有的不需要写版本号,spring boot已经做最合理的版本处理。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring-boot-starter-web帮我们导入了spring web模块正常运行所需要的组件。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.2.4.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>2.2.4.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.2.4.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>2.2.4.RELEASE</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>tomcat-embed-el</artifactId>
<groupId>org.apache.tomcat.embed</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.3.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.3.RELEASE</version>
<scope>compile</scope>
</dependency>
主程序类
package com.roger.helloworld;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBoot01HelloworldApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBoot01HelloworldApplication.class, args);
}
}
@SpringBootApplication:spring boot应用标注在某个类上说明这个类是spring boot主配置类。spring boot就应用运行这个类的main()方法类启动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的配置类。@Configuration,配置类-也是容器中的一个组件。@Compent
@EnableAutoConfiguration:开启自动配置功能。
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
@AutoConfigurationPackage:自动配置包
@Import({Registrar.class}) 给容器导入一个组件(AutoConfigurationPackages.Registrar)。
将主配置类(SpringBootApplication)所在的包(com.roger)及下面所有的子包里面的所有组件扫描到springboot容器中,放到其他目录下的组件将不能自动被扫描到。
@Import({AutoConfigurationImportSelector.class})
AutoConfigurationImportSelector,导入哪些组件的选择器,将所有需要导入的组件以全类名的形式返回,这些组件就会添加到容器中。
会给容器中导入非常多的自动配置类(XXXAutoConfiguration),就给容器中导入这个场景所需要的所有组件,并配置好这些组件。
Spring boot启动的时候从类路径中的META-INF/spring.factories中获取EnableAutoConfiguration指定的值。将这些值作为自动配置类导入到容器中,自动配置类就生效,帮我们进行自动配置工作。
本文档为查看驰狼课堂的《Spring Boot视频教程(上)核心技术篇》所做的学习笔记,课堂地址: