Springboot基本原理

一、springboot概述

  1. springboot是快速使用spring的方式,而不是spring的增强

  1. Springboot基于约定优于配置,让开发人员不必在配置与逻辑业务之间进行思维切换,专注与逻辑代码的编写,缩短了开发周期

  1. 自动配置:Springboot自动配置(运行时,自动考虑那些应该配置,那些不用配置)

  1. 起步依赖:将常用一些库及依赖进行了打包,运行时候只需要调用这个打包就行

  1. 非功能性特性:Springboot提供了常见的非功能性特性,比如嵌入式服务器(例如tomcat),内置到了框架中

二、启动流程

  1. 创建maven项目

  1. 导入启动依赖

  • 继承parent

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>

这个时候再导入我们需要的springboot starter时,就可以忽略版本号:

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
</dependency>
  • import导入

 
<dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>1.5.4.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
</dependencyManagement>

三、定义Controller

  • 导入注解@restcontroller,@requestmapping(“/xxxx”)

四、书写引导类

  • xxxxapplication(导入注解springbootapplication)项目的入口

@SpringBootApplication
public class MySpringbootApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(MySpringbootApplication.class, args);
    }
 
}


Springboot很多配置都有默认值,想使用自己的配置就可以在 properties或者yml或者yaml进行配置(优先级也是这个顺序递减)

yaml语法(yml语法):切记冒号后面必须有空格

server:
  port: 8081
  servlet:
    context-path: /mango

properties语法如下:

# 设置服务器端口号
server.port=8081
# 修改上下文路径
server.servlet.context-path=/mango


读取配置内容

  • @value

  • Environment(@autowired注入spring容器)

  • @configurationProperties

创建实体类用@compoent注解识别
@configurationProperties,识别基础信息
@configurationProperties(prefix=“xxxx”)定义具体的对象信息

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值