Spring Boot项目基础配置

275 阅读2分钟
  • 自定义打包方式
  1. 首先去除pom文件中的原有的parent
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.2.2.RELEASE</version>
	<relativePath/>
</parent>
  1. 方便开发微服务项目或者多模块项目时如需要对项目依赖版本进行统一管理,就需要使用dependencyManagement来实现了。在原dependencies外层增加dependencyManagement标签,需注意的是,如果使用dependencyManagement来管理依赖,那么子项目中需声明父类项目中的依赖才能被使用,如果父项目中直接使用dependencies来管理依赖,那么所有基层此父项目的子项目将拥有父项目中的所有依赖。
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.0.5.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
  1. 因为去掉了spring-boot-starter-parent的继承,所以我们需要手动设置Java版本及编码格式等
//设置编码格式
<properties>
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
	<java.version>1.8</java.version>
</properties>
//自定义打包,在原plugins里面添加一个plugin
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
    </plugins>
</build>
  • Tomcat基础配置:此处演示的代码为yaml格式,与properties只是格式有区别,其它是一样的效果
server:
  servlet:
    #表示项目名称,不配置时默认为/,如果配置了,需在访问路径中添加配置的路径
    context-path: /demo
    session:
      #session失效时间,默认为此处配置30分钟
      timeout: 30m
  #配置Web容器端口号
  port: 8088
  error:
    #项目出错时跳转路径
    path: /error
  tomcat:
    #tomcat最大线程数,默认200
    max-threads: 300
    #存放tomcat运行日志和临时文件的目录,若不配置,则默认使用系统的临时目录
    #basedir:
    #tomcat请求编码
    uri-encoding: utf-8
  • 通过配置文件配置类属性 yml文件配置单个类属性
student:
  name: 测试
  birth_date: 2019-12-25
  cource:
    - 语文
    - 数学
    - 英语

Java类:

import java.util.List;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Getter
@Setter
@ToString
@Component
@ConfigurationProperties(prefix = "student")
public class Student {

    private String name;

    private String birthDate;

    private List<String> cource;
}

测试:

import com.lei.tang.demo.domain.Student;
import com.lei.tang.demo.domain.Students;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    Student student;

    @GetMapping("/one")
    public String getOne(){
        return student.toString();
    }
}

复杂配置:

many:
  students:
  - name: 张三
    birth_date: 2018-12-25
    cource:
    - 语文
    - 数学
    - 英语
  - name: 李四
    birth_date: 2000-12-25
    cource:
    - 语文
    - 数学
    - 音乐

对应的Java Bean

import java.util.List;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Getter
@Setter
@ToString
@Component
@ConfigurationProperties(prefix = "many")
public class Students {

    private List<Student> students;
}

测试代码:

import com.lei.tang.demo.domain.Student;
import com.lei.tang.demo.domain.Students;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    Students students;
    
    @GetMapping("/many")
    public String getMany(){
        return students.toString();
    }
}

更多文章: 点击跳转CSDN博客 点击跳转简书博客 公众号:代码小搬运

扫码关注代码小搬运公众号