Springboot基本构建

Springboot启动流程

加载配置文件 application.properties
自动装配 artifactid(pom.xml文件中)
加载组件 @Repository @Service @Controller @Componet @Entity
应用初始化
ps:必须安装JDK8以上版本
SpringIntializr配置更方便

Springboot目录结构

在这里插入图片描述

Springboot入口类

运行此类即可运行Springboot工程

package com.imooc.myspringboot;

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

@SpringBootApplication
public class MyspringbootApplication {

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

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>

    <groupId>com.imooc</groupId>
    <artifactId>myspringboot</artifactId>
    <version>1.0-SNAPSHOT</version>
  //创建基础应用  引入springboot中的基础组件  所有springboot依赖于此
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>
    <dependencies>
      //starter启动器 增加支持javaweb的能力 不写版本号因为<parent>标签已经预写
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
   //打包的方式  以插件的方式实现  自动将所有的类和资源整合成一个独立可运行的jar包
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
Springboot支持两种配置文件
1 application.properties

常用配置
端口 serve.port=80
设置应用上下文 serve.servlet.context-path=/
日志文件输出路径 logging.file=
最低日志输出级别 logging.level.root=
是否开启调试模式 debug=true/false
数据库相关配置 spring.datasourece
在tomcat中有多个应用 对不同的url进行区分 通过设置应用上下文进行区分
debug方便程序的调试 程序底层执行的细节和流程全部打印在控制台和日志文件中 方便我们进行跟踪 程序启动生成报告(组件的加载)
在这里插入图片描述

2 application.yml
2.1 YAML非标记语言

格式 key:(空格)value
使用空格代表层级关系以 “:” 结束
在这里插入图片描述

2.2yml中的其他配置

Springboot允许我们自定义应用配置项 在程序运行时允许动态加载 将项目的自定信息放在配置文件中
默认的自定义选项
class中的属性数据类型与yml中的数据类型相对应可以转换
@Value(“${ }”)在启动时自动的扫描yml配置文件将对应的key注入进来

yml
在这里插入图片描述
class

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

@Controller
public class MyController {
    @Value("${mall.config.name}")
    private String name;
    @Value("${mall.config.description}")
    private String description;
    @Value("${mall.config.hot-sales}")
    private Integer hotSales;
    @Value("${mall.config.show-advert}")
    private Boolean showAdvert;

    @RequestMapping("/info")
    @ResponseBody
    public String info(){
        return String.format("name:%s,description:%s,hot-sales:%s,show-advert:%s",
                name,description,hotSales,showAdvert);
    }
}

2.3 环境配置文件

springboot可以针对不同的环境提供不同的profile
pofile的默认文件命名格式为application-{env}.yml
sring.profiles.active选项来指定不同的profile
active:可以修改开发/生产环境 使用的application配置文件 (active:后面填写applicaton后面的后缀)
application.yml
在这里插入图片描述
application-dev.yml
在这里插入图片描述
application-prd.yml
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值