SpringBoot入门篇(1)简介与HelloWorld

2 篇文章 0 订阅
2 篇文章 0 订阅

一、SpringBoot简介

1. SpringBoot简化Spring应用开发。

随着时间的推移,Spring开发越来越笨重,需要众多繁琐的配置,开发和部署流程都变得复杂,集成第三方的框架也变得很难。SpringBoot的推出就是为了解决上述问题而诞生。

2. Spring、SpringBoot、SpringCloud

  • 最开始Spring全家桶的时代,有众多Spring相关组件。
  • SpringBoot:J2EE一站式的解决方案,是产品级别的应用。
  • SpringCloud:分布式的整体解决方案。

二、SpringBoot特点

  • 快速创建单体Spring应用
  • 嵌入Tomcat,Jetty或Undertow(无需部署WAR文件)
  • 提供 starter ,以简化依赖和版本控制,方便构建配置
  • 尽可能自动配置Spring和第三方框架。
  • 提供可用于生产的功能,比如监控和外部化配置
  • 开箱机用,没有代码生成,也不需要XML配置

三、环境准备

  • IntelliJIDEA 2019.2
  • Maven3.3.3
  • jdk1.8: 1.8.0_144
  • SpringBoot 2.2.2.RELEASE

Maven配置

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <pluginGroups>
  </pluginGroups>
  <proxies>
  </proxies>
  <servers>
  </servers>

  <mirrors>
    <mirror>
      <id>alimaven</id>
      <mirrorOf>central</mirrorOf>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    </mirror>
    <mirror>
      <id>repo1</id>
      <mirrorOf>central</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://repo1.maven.org/maven2/</url>
    </mirror>
    <mirror>
      <id>repo2</id>
      <mirrorOf>central</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://repo2.maven.org/maven2/</url>
    </mirror>
  </mirrors>

  <profiles>
    <profile>
      <id>jdk18</id>
      <activation>
        <jdk>1.8</jdk>
        <activeByDefault>true</activeByDefault>
      </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>

</settings>

四、项目构建与测试

1、新建项目
  • 新建一个项目springboot-helloworld
2、POM配置
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.2.RELEASE</version>
  </parent>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
3、编写启动类
@SpringBootApplication
public class HelloWorldApp {

    public static void main(String[] args) {
        
        SpringApplication.run(HelloWorldApp.class, args);
    }
}
4、编写Controller

在controller包下新建

@RestController
public class HelloController {

    @GetMapping(value = "/hello")
    public String hello() {
        return "Hello World";
    }
}
5、运行并测试项目

运行项目,可以直接运行,也可以生成Jar包用java -jar命令运行

在浏览器输入 http://localhost:8080/hello ,即可得到项目结果,应用得到预期的结果。
测试结果

五、原理解析

1、POM

父POM

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.2.RELEASE</version>
  </parent>
  
他的父项目是
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.2.2.RELEASE</version>
    <relativePath>../../spring-boot-dependencies</relativePath>
  </parent>

spring-boot-starter-web

   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

spring-boot-starter是场景启动器,帮助我们自动导入依赖的组件,其中spring-boot-starter-web就自动导入了web的相应依赖

spring-boot-maven-plugin

      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>

简化部署,这个插件可以把应用打包成一个可以直接执行的Jar包,相应的依赖也会打到Jar包中,只需要执行java -jar命令就可以执行。

2、主启动类

2.1 @SpringBootApplication: Spring Boot应用标注在某个类上说明这个类是SpringBoot的主配置类,SpringBoot 就应该运行这个类的main方法来启动SpringBoot应用

//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) })
public @interface SpringBootApplication {

从@SpringBootApplication来看,已经包含了**@EnableAutoConfiguration@SpringBootConfiguration**,开启了自动配置

2.2 @EnableAutoConfiguration:开启了自动配置,

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {

通过定义可以看到,@EnableAutoConfiguration包含了@AutoConfigurationPackage注解和@Import注解

@AutoConfigurationPackage:自动配置包

@Import:导入资源注解,@Import(AutoConfigurationImportSelector.class)导入AutoConfigurationImportSelector类

2.3 @SpringBootConfiguration注解标明这是个SpringBoot的配置类,包含了@Configuration,配置类还包含了@Component。

3、Controller类

@RestController:注解相当于@Controller和@ResponseBody组合注解
@Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 对象。
@responseBody 注解的作用是将controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON或者是XML数据。

参考文档:

SpringBoot官方:https://spring.io/projects/spring-boot

尚硅谷文档

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值