springboot入门--第一个springboot程序

环境配置

  环境约束:

  -jdk1.8:Spring Boot 建议jdk1.7以上;java -version   java version "1.8.0_131"

  -maven3.x:maven3.3以上版本;Apache Maven 3.6.1

 

SpringBoot HelloWorld

  一个功能:浏览器发送hello请求,服务器接收请求并相应,响应Hello World 字符串;

1、创建一个maven工程;(jar)

 

 

 

 

 

 

 

 

 

 

2、导入依赖springboot相关的依赖

 

 

 

 

 <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.13.RELEASE</version>
    </parent>

    <!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

可以reload一下maven:

 

3、编写一个主程序,启动springboot应用

新建HelloWorldMainJavaApplication和controller(注意目录结构,Application默认只加载Application.java所在包及其子包下的内容)。

 

package com.wu;


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

/**
 * @SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用
 */

@SpringBootApplication
public class HelloWorldMainJavaApplication {

    public static void main(String[] args) {
        //spring应用启动起来
        SpringApplication.run(HelloWorldMainJavaApplication.class,args);
    }
}

 

package com.wu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class HelloController {
    /**
     * 接收来自浏览器的hello请求
     */
    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return "HelloWorld!";
    }
}

运行HelloWorldMainJavaApplication主函数:

 

4、项目探究

①POM文件

 

    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.13.RELEASE</version>
    </parent>

 

2、导入的依赖

复制代码

<!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

复制代码

spring-boot-starter-web

spring-boot-starter:spring-boot场景启动器;帮我们导入了web模块正常运行所依赖的组件

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

要什么功能就导入什么场景启动器

 

②主程序类,主入口类

 

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

 

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

@SpringBootConfiguration:Spring Boot得配置类;

    标注在某个类上,表示这是一个Spring Boot的配置类;

@Configuration:配置类上来注解这个配置

     配置类 ----- 配置文件;配置类也是容器的一个组件;@Component

 

@EnableAutoConfiguration:开启自动配置功能;以前我们要配置的东西,Spring Boot帮我们自动配置; @EnableAutoConfiguration

             告诉SpingBoot开启自动配置功能;这样自动配置才能生效

@org.springframework.boot.autoconfigure.AutoConfigurationPackage
@org.springframework.context.annotation.Import({org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {

  @AutoConfigurationPackage:自动配置包

     @org.springframework.context.annotation.Import({org.springframework.boot.autoconfigure.AutoConfigurationPackages.Registrar.class}):spring的底层注解

     @import给容器中导入一个组件;导入的组件由AutoConfigurationPackages.Registrar.class指定

 

 六、使用Spring Initializer 快速创建Spring Boot项目 

 IDEA都支持使用Spring的项目创建向导快速创建一个Spring Boot项目;

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 默认生成的Spring Boot项目的特点:

  • 主程序已经生成好了,只需要编写自己的业务逻辑就好了
  • resources文件夹目录结构
    • static:保存所有的静态资源;js css img
    • templates:保存所有的模板页面;(Spring Boot 默认jar 包使用嵌入式的Tomcat,默认不支持JSP页面);可以使用模板引擎(freemaker,thymeleaf);
    • application.properties:Spring Boot应用的配置文件;可以修改默认配置

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值