01_SpringBoot入门

SpringBoot简化了Spring应用的开发过程,通过约定大于配置的原则,帮助开发者快速创建独立的应用。本文介绍如何利用SpringBoot搭建第一个应用,并讲解其核心概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringBoot入门

SpringBoot 用来简化 Spring 应用开发, 约定大于配置, 去繁从简, justrun 就能创建一个独立的,产品级别的应用.

背景 :

J2EE 开发笨重, 配置繁多, 开发效率低, 部署流程复杂, 第三方技术集成难度大.

优点 :

  • 快速创建独立运行的 Spring 项目以及与主流框架集成.
  • 使用嵌入式的 servlet 容器,应用无需打成 war 包.
  • starters 自动依赖于版本控制.
  • 大量的自动配置, 简化开发, 也可修改默认设置.
  • 无需配置 xml, 无代码生成, 开箱即用.
  • 生产环境运行时应用监控.
  • 与云计算的天然集成.

微服务 :

微服务是一种架构风格

以前 : ALL IN ONE

​ 问题 : 牵一发而动全身

​ 优点 : 部署 调试 容易

在这里插入图片描述

微服务 :

​ 每一个功能元素都是以个可独立替换和独立升级的单元.

在这里插入图片描述

HelloWorld :

浏览器发送 hello 请求, 服务器接收请求并处理, 响应 Helloworld 字符串.

1. 创建一个 maven 工程(jar)

在这里插入图片描述

2. 导入 SpringBoot 相关的依赖

在这里插入图片描述

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
</parent>

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

3. 编写一个主程序

package com.lfy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 *  @SpringBootApplication
 *  标注一个主程序类,说明是一个 Springboot 应用
 */
@SpringBootApplication
public class HelloWorldMainApplication {
    public static void main(String[] args) {
        //  Spring应用启动起来
        SpringApplication.run(HelloWorldMainApplication.class,args);
    }
}

4. 编写Controller

@Controller
public class HelloController {
    @ResponseBody
    @RequestMapping("/hello")
    public String hell0(){
        return "HelloWorld";
    }
}

5. 运行测试

在这里插入图片描述

6. 简化部署

<!-- 这个插件,可以将应用打包成一个可执行的jar包 -->
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

打成 jar 包 :

在这里插入图片描述

在这里插入图片描述
在命令行运行 java -jar 命令启动应用

在这里插入图片描述

访问测试 :

在这里插入图片描述

HelloWorld 探究 :

1. POM 文件

父项目 :
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
</parent>

父项目的父项目 :

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>1.5.9.RELEASE</version>
    <relativePath>../../spring-boot-dependencies</relativePath>
</parent>
<!-- 
	里面定义了默认的版本 他真正管理了Springboot应用的版本 
	因此以后导入不需要声明版本号
 	如果有 SpringBoot 没有管理的依赖需要自己管理版本
-->
<activemq.version>5.14.5</activemq.version>
<antlr2.version>2.7.7</antlr2.version>
<appengine-sdk.version>1.9.59</appengine-sdk.version>
<artemis.version>1.5.5</artemis.version>
<aspectj.version>1.8.13</aspectj.version>
<assertj.version>2.6.0</assertj.version>
<atomikos.version>3.9.3</atomikos.version>
导入的依赖 :
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
</dependency>

spring-boot-starter-web :

​ spring-boot-starter : 场景启动器

​ spring-boot-starter-web : 帮我们导入了web模块正常运行所依赖的组件

在这里插入图片描述

SpringBoot 将所有的功能场景都抽取出来,做成一个个的启动器(starters)启动器,只需要在项目里面引入这些starter相关的场景的所有依赖都会导入进来,要用什么功能就导入什么启动器.

2. 主程序类

/**
 *  @SpringBootApplication
 *  标注一个主程序类,说明是一个 Springboot 应用
 */
@SpringBootApplication
public class HelloWorldMainApplication {
    public static void main(String[] args) {
        //  Spring应用启动起来
        SpringApplication.run(HelloWorldMainApplication.class,args);
    }
}

@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 {

@SpringBootConfiguration 注解 : SpringBoot 的配置类

​ 标注在哪个类,哪个类就是 Springboot 的配置类.

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {

@Configuration 注解 : 标注Spring的配置类

​ 标注在哪个类,哪个类就是配置类,同时配置类也是一个组件因为有标注

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {

@EnableAutoConfiguration 注解 :

​ 开启自动配置,以前需要配置的东西,Springboot帮我们自动配置,@EnableAutoConfiguration告诉 Springboot 开启自动配置功能.

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

@AutoConfigurationPackage 注解 :

自动配置包.

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
//将主配置类(@SpringBootApplication标注的类)的所在包下面以及包下面的
 //所有子包里面的组件扫描到Spring 容器中.
@Import(AutoConfigurationPackages.Registrar.class)

AutoConfigurationPackages.Registrar.class , 将主配置类(@SpringBootApplication标注的类)的所在包下面以及包下面的所有子包里面的组件扫描到 Spring 容器中.

@Import(EnableAutoConfigurationImportSelector.class) :

​ EnableAutoConfigurationImportSelector 导入哪些组件的选择器,将所有需要的组件以全类名的方式返回,这些组件就会被添加到容器中.会给容器中导入非常多的自动配置类(xxxAutoConfiguration),这就是给容器中导入这个场景所需要的所有组件,并配置好这些组件.

​ 有了自动配置类,我们就免去了手动编写配置注入功能组件等工作.

SpringBoot在启动时从类路径下的 META-INF/spring.factories 中获取 EnableAutoConfiguration 指定的值,将这些值作为自动配置类导入到容器中,自动配置类就生效,帮我们进行自动配置工作. 以前我们需要自己配置的东西,自动配置类帮我们配置.

J2EE 的整体整合方案和自动配置都在 spring-boot-autoconfigure-1.5.9.RELEASE.jar中.

使用 IDE 的向导快速创建Springboot应用

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

目录结构 :

在这里插入图片描述

resources文件夹中目录结构 :

  • static : 保存所有的静态资源 : js css images
  • templates : 保存所有的模板页面 (SpringBoot默认jar包使用嵌入式的tomcat,默认不支持jsp页面,可以使用模板引擎 freemarker Thymeleaf 等)
  • application.properties : Spring应用的配置文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值