SpringBoot初探

1. 什么是SpringBoot

SpringBoot并不是一个新的技术框架,只是在Spring的基础上进行封装,它有以下几个特点:

  1. 能够非常方便快捷的创建一个Spring项目
  2. 内置Tomcat、Jetty等Web容器
  3. 自动装配,能够很好的集成一些框架
  4. 只需要很少的配置文件即可运行

2. SpringBoot项目的构建方式

2.1 通过官网生成

https://start.spring.io/ 快速生成

2.2 IDEA在线模板生成

本质上跟官网生成一样,只不过是在IDEA中操作的

2.3 通过Maven项目构建

  1. 创建Maven项目
  2. 引入SpringBoot依赖
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.15.RELEASE</version>
        <relativePath/>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
  1. 创建启动类
package com.wxw;

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

@SpringBootApplication
public class WmSpringBootApplication {

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

}
  1. 运行启动类main方法即可。

3. SpringBoot中的常规配置

3.1 配置文件

SpringBoot给我们提供了两种格式的配置文件application.properties,application.yml,我们可以选择其中的一种进行相关的配置。
例如Tomcat配置修改:

server.port=8082
server.servlet.context-path=/springboot

3.1.1 中文乱码配置

server.tomcat.uri-encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.messages.encoding=UTF-8

在这里插入图片描述

3.1.2 自定义属性

# 自定义属性
user.username=wangmian
user.age=28
user.address=湖南郴州

在代码中通过@Value注解设值

    @Value("${user.username}")
    private String username;
    @Value("${user.age}")
    private Integer age;
    @Value("${user.address}")
    private String address;

如果系统中用到该属性的地方比较多,则可以使用对象配置的方式进行封装。
引入依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

创建配置类:

package com.wxw.pojo;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
// 与属性中的user前缀属性绑定
@ConfigurationProperties(prefix = "user")
public class UserPojo {

    private String username;
    private Integer age;
    private String address;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "UserPojo{" +
                "username='" + username + '\'' +
                ", age=" + age +
                ", address='" + address + '\'' +
                '}';
    }

}

3.2 Logback日志配置

SpringBoot有内置的Logback日志依赖,直接在属性文件中进行配置即可使用。

# logback的配置
logging.file=d:/log.log
logging.level.org.springframework.web=DEBUG

3.3 Profile多环境配置

属性文件命名规则:application-xxx.properties
在主配置文件中进行配置:

spring.profiles.active=xxx # 指定对应的环境

4. SpringBoot中的静态资源

4.1 static目录

SpringBoot默认的存放静态资源的目录
在这里插入图片描述

4.2 webapp目录

在resources同级目录下创建一个webapp目录,该目录的类型必须是ResourcesRoot
在这里插入图片描述

4.3 自定义静态资源目录

自定义目录后,创建对应的相关资源,然后在属性文件中去覆盖静态资源的路径配置即可

# 表示所有的访问都经过静态资源路径
spring.webflux.static-path-pattern=/**
# 覆盖默认的配置,所有需要将默认的static public等这些路径将不能作为静态资源的访问
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/custom
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值