SpringBoot入门
SpringBoot
SpringBoot是什么?
Spring Boot是由Pivotal团队提供的全新框架(和Spring4一起诞生),其设计目的是用来简化新Spring应用的初始搭建以及开发过程,该框架使用了特定的方式来进行配置,就像maven整合了所有的jar包,简单的说,spring boot就是整合了很多优秀的框架,不用我们自己手动的去写一堆xml配置然后进行配置。
SpringBoot的特点
基于Spring,开发者可以快速入门
SpringBoot可以创建独立运行的应用而不依赖于容器
SpringBoot内置tomcat,不需要打包成war包
提供maven极简配置,缺点就是会引入很多你不需要的包
简化配置,不用再看过多的xml
构建一个SpringBootDemo
创建项目 File---->New---->project---->Spring Initializr
输入maven的group 和artifact。
type选择Maven
jdk选择1.8
选择starter,通过搜索找到web 并勾选。点击完成
点击finish 按钮。创建项目如下:
pom.xml
<?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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.xpx</groupId> <artifactId>springbootdemo01</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springbootdemo01</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <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> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
这是Spring Boot的父级依赖,这样当前的项目就是Spring Boot项目了
它用来提供相关的Maven默认依赖。使用它之后,常用的包依赖可以省去version标签
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </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> <scope>test</scope> </dependency>
编写HelloSpringBoot
@RestController
public class HelloController {
@RequestMapping("hello")
public String hello(){
return "Hello SpringBoot";
}
}
启动项目
@SpringBootApplication:是一个复合注解,包括 @ComponentScan,和@SpringBootConfiguration,@EnableAutoConfiguration。
@EnableAutoConfiguration 的作用启动自动的配置,@EnableAutoConfiguration注解的意思就是SpringBoot根据你添加的jar包来配置你项目的默认配置,比如根据spring-boot-starter-web ,来判断你的项目是否需要添加了webmvc和tomcat,就会自动的帮你配置web项目中所需要的默认配置。
@ComponentScan:扫描当前包及其子包下被@Component,@Controller,@service,@Repository注解标记的类并纳入到spring容器中进行管理。是以前的<context:component-scan>
@SpringBootConfiguration:注当前类是配置类,并会将当前类内声明的一个或多个以@Bean注解标记的方法的实例纳入到Spring容器中,并且实例名就是方法名
@SpringBootApplication
public class Springbootdemo01Application {
public static void main(String[] args) {
SpringApplication.run(Springbootdemo01Application.class, args);
}
}
然后在浏览器访问http://localhost:8888/hello,就能看到 "Hello SpringBoot"。