SpringBoot环境搭建
SpringBoot环境搭建环境搭建的俩种方式
方式一:使用Maven构建项目
1.创建一个maven项目

把不需要的依赖删除

在pom.xml中添加父项目
<!--继承springboot的父项目-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
引入依赖
<!--web的启动器-->
<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>
</dependency>
创建maven结构
在main文件夹下新建一个文件夹java

在main文件夹下新建一个resources文件夹

2.开发一个main函数
src/main/java
com.nylg 主包
entity 子包
service
controller
注意: main 入口类(入口类必须要放在主包下和子包同级)
结构如图


3.配置文件
选中resources,快捷键alt+insert,选择file,创建application.yml,必须出现树叶+绿色按钮才能正常使用
项目目录结构src/main/resources/application.yml
语法:
层级之间有一个tab键的间隔
属性和值之间有一个空格
注意:springboot默认访问时没有项目名,默认端口号为8080
4.测试类
@Controller
@RequestMapping("/test")
public class TestController {
/*springboot默认支持的视图层模板是thmyleaf,不支持jsp*/
@RequestMapping("/test")
@ResponseBody
public String test(){
return "index";
}
}
application.yml
server:
port: 8989
context-path: /springboot
运行展示

springboot启动报错,端口号被占用,需要修改端口号

方式二:使用Spring Initializr 构建项目


需要在pom.xml添加一个测试启动器的依赖
SpringBoot的启动方式
1.在main中直接运行
2.jar包运行
(1)在pom.xml添加maven插件
<build>
<plugins>
<!-- 这个插件可以将maven打包为一个可执行的jar包 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
(2)打成jar包
选择maven --- lifestyle(生命周期)-- package 打包完成 在tarage目录下
cmd 命令行窗口 ---》切换到jar包所在位置 ----》执行java -jar jar包名之后就可以运行了
spring-boot-start 探究
它的父项目:版本仲裁,以后导入依赖不需要写版本号
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
spring-boot-start-web:
spring-boot 场景启动器:帮我们导入了web模块正常运行所依赖的组件
SpringBoot将所有的功能场景都抽取出来,做成一个个starters(启动器),只需要在项目里面引入这些start相关场景的所有依据都会导入进来,要用什么功能,就导入什么功能
@SpringBootConfiguration spring boot配置类
标注在某个类上,表示这个是springboot配置类
@Configuration 配置类上来标注这个注解:
配置类---配置文件 配置类也是容器中的一个组件:@Component
@EnableAutoConfiguration:开启自动配置功能
以前我们需要配置的东西,SpringBoot帮我们自动配置;@EnableAutoConfiguration告诉springboot开启自动配置功能:这样自动配置才能生效


浙公网安备 33010602011771号