9

【SpringBoot实战】核心配置和注解 - hjk-airl

 3 years ago
source link: https://www.cnblogs.com/hjk-airl/p/16087347.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

SpringBoot核心配置在springboot中有非常重要的作用,我们可是使用核心配置文件进行一些基础功能的定义,属性值的注入等。springboot支持两种格式的核心配置文件,一种是properties和yaml,properties采用传统的键值对方式,但是相比于yaml优先级更高,yaml采用采用树状结构,使用缩进进行关系判断。
注解可以说是springboot的一大特点,采用注解极大的简化了spring的配置问题。

我们采用上篇博客简单spring initializr方式创建springboot项目,可参考 https://www.cnblogs.com/hjk-airl/p/16081134.html

application.properties和application.yaml

application.properties和application.yaml作用是一样的,对于不同的人群有不一样的偏好,可采用不同的方式,看个人喜好。

  • 我们在resources文件夹下创建一个application.yaml文件,方便同时对别两种方式的不同,因为我们在创建项目的时候application.properties已经创建,所以只需要创建yaml就行。
  • 步骤(右键resources->new->file->输入application.yaml)一定要是这个文件名,不然springboot不识别,创建完成后可以看到这个文件的图标和application.properties文件一样。

springBoot基本系统属性配置

我们先修改一下端口号看一下,我们知道默认的端口号是8080,我们修改为8081然后运行。

server.port=8081

image

在这值得注意的是当我们同时存在application.properties和application.yaml时,并且两者都有内容时,会优先使用properties文件,我们在application.properties文件内容删除或者注释掉在application.yaml里输入

server:
  port: 8082

我们可以在配置文件里配置一些基本信息如jdbc的环境变量,和开启松散绑定,开启什么什么缓存的配置等。

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    password: 123456
    username: root

我们还可以配置一些自己类的属性。
我们创建一个Person类,我们在加@ConfigurationProperties时idea可能会爆红,但是对程序影响,我们可以先在pom.xml里加一个依赖,如果还爆红那就把idea关闭在重新打开。

	<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
        </dependency>
package com.hjk.springboot01.pojo;

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

@Component //将Person类作为Bean注入Sprng容器
@ConfigurationProperties(prefix = "person")
public class Person {
    private String name;
    private String address;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

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

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

然后在编辑controller类

package com.hjk.springboot01.controller;

import com.hjk.springboot01.pojo.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PersonController {

    @Autowired
    public Person person;

    @GetMapping("/person")
    public String getPerson(){
        System.out.println(person.toString());
        return person.toString();
    }

}

最后我们在核心配置文件application.properties或者application.yaml里编写想要配置的东西,我没配置Person的属性。
application.properties

person.name='hjk'
person.address='中国'

application.yaml
还是那个问题,如果你同时存在properties和yaml文件,并且两者都有同种属性,那么会优先使用properties里面的。

person:
  name: hjk
  address: hahaha

最后我们访问当本地url可以在控制台看到输出。
对比两种注入方式

对比点 @Configuration @Value

底层框架 springboot spring

功能 批量注入配置文件属性 单个注入

setter方法 需要 不需要

复制类型注入 支持 不支持

松散绑定 支持 不支持

JSR303数据校验 支持 不支持

SpEL表达式 不支持 支持

自定义配置

如果使用@PropertySource和@Configuration注解实现,@PropertySource注解可以指定自定义配置文件的名称,@Configuration注解可以将实现类指定为自定义配置类,如果需要将自定义配置文件中的属性值注入实体类属性,可以使@ConfigurationProperties或@Value注入属性值

  • 自定义配置文件,我们创建文件My.properties
    并且在里面输入
my.name=hjk
my.address=hehehe

修改Person类

package com.hjk.springboot01.pojo;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:My.properties")
@EnableConfigurationProperties(Person.class)
@ConfigurationProperties(prefix = "my")
public class Person {
    private String name;
    private String address;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

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

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

@Configuration 表示自定义配置类,这个类会作为bean组件添加到spring容器种

@PropertySource 指定自定义配置文件的位置和名称

@ConfigurationProperties 将配置类中以指定开头的属性值注入配置类属性

@EnableConfigurationProperties 开启配置的属性注入功能,配合@ConfigurationProperties使用

使用Profile进行多环境配置

在实际开发中,应用程序通常需要部署到不同的环境中,比如开发环境、测试环境、生产环境。不同的环境可能需要不同的配置,针对这种情况,显然手动修改配置文件适应不同开发环境很不现实,这样我们就可以使用项目的多环境配置。

使用Profile进行多环境配置

使用Profile进行多环境配置命名必须是固定格式。

application-dev.properties
application-test.properties
application-prod.properties

如果想使用上述对应环境的配置文件,需要在全局配置文件里开启指定的配置文件即可。
有两种方式:
第一种:在控制台输入

java -jar xxx.jar --spring.profiles.active=dev

第二种:在application.properties或者yaml中开启

spring.profiles.active=dev

  • 使用注解@Profile进行多环境配置,这里就不写了。

主要记录了springboot的核心配置和相关注解,包括全局配置使用,配置文件属性值的注入、springboot自定义配置、多环境配置,这些都是需要我们掌握的基本技能。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK