2021-4-28:Spring Boot两种全局配置和两种注解

本文介绍了Spring Boot中Application.properties配置文件的使用,包括如何添加配置、处理对象类型的配置以及@ConfigurationProperties和@Value注解的区别。通过示例展示了在Person和Pet类中配置对象属性,并探讨了两种注解在注入复杂类型时的不同行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Application.properties配置文件

(一)创建Spring Boot的Web项目PropertiesDemo
利用Spring Initializr方式创建项目
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在application.properties里添加相关配置

点开resource目录,查看应用程序属性配置文件
在这里插入图片描述

#修改tomcat默认端口号
server.port=8888
#修改web虚拟路径
server.servlet.context-path=/lzy

运行项目,查看结果
在这里插入图片描述
对象类型的配置与使用
(1)创建Pet类
在这里插入图片描述
在这里插入图片描述

package net.lhf.lesson03.bean;

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

/**
 * 功能:宠物类
 * 作者:李红芙
 * 日期:2021年04月28日
 */
public class Pet {
    private String type; //类型
    private String name; //名字

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

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

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

创建Person类
在这里插入图片描述

package net.lhf.lesson03.bean;

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

import java.util.List;
import java.util.Map;

/**
 * 功能:人类
 * 作者:李红芙
 * 日期:2021年04月28日
 */
public class Person {
    private int id;
    private String name; //姓名
    private List hobby; //爱好
    private Map family; //家庭成员
    private Pet pet; //宠物

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public List getHobby() {
        return hobby;
    }

    public void setHobby(List hobby) {
        this.hobby = hobby;
    }

    public Map getFamily() {
        return family;
    }

    public void setFamily(Map family) {
        this.family = family;
    }

    public Pet getPet() {
        return pet;
    }

    public void setPet(Pet pet) {
        this.pet = pet;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", hobby=" + hobby +
                ", family=" + family +
                ", pet=" + pet +
                '}';
    }
}

在application.properties里配置对象

#配置对象
person.id=1
person.name=张三丰
person.hobby=旅游,美食,音乐
person.family.father=张云光
person.family.mother=吴文燕
person.family.grandpa=张宏宇
person.famliy.grandma=唐雨欣
person.family.son=张君宝
person.family.daughter=张晓敏
person.pet.type=泰迪犬
person.pet.name=瑞瑞

在这里插入图片描述
给Person类添加注解
添加注解@Component,交给Spring去管理
在这里插入图片描述
添加注解@ConfigurationProperties(prefix = “person”)
在这里插入图片描述
未配置注解处理器,暂时不管,看它一会会不会影响运行

给Pet类添加注解
添加注解@Component,交给Spring去管理
添加注解@ConfigurationProperties(prefix = “person.pet”) - 可以不用添加
在这里插入图片描述
从Spring容器里获取Person类的实例并输出
实现接口ApplicationContextAware,实现其抽象方法setApplicationContext
在这里插入图片描述
在这里插入图片描述

package net.lhf.lesson03;

import org.junit.jupiter.api.Test;
import org.springframework.beans.BeansException;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

@SpringBootTest
class PropertiesdemoApplicationTests implements ApplicationContextAware {

    private ApplicationContext context;//声明应用容器

    @Test
    void contextLoads() {
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException{
       context = applicationContext;
    }


}

在这里插入图片描述
运行测试,出现乱码
在这里插入图片描述
解决输出结果的汉字乱码问题
使用JDK工具native2ascii.exe将汉字处理成uncode编码
在这里插入图片描述

Microsoft Windows [版本 10.0.18363.1379]
(c) 2019 Microsoft Corporation。保留所有权利。

D:\java\JSP\PropertiesDemo>cd src/main/resources

D:\java\JSP\PropertiesDemo\src\main\resources>native2ascii -encoding utf8 application.properties
#\u4fee\u6539tomcat\u9ed8\u8ba4\u7aef\u53e3\u53f7
server.port=8888
#\u4fee\u6539web\u865a\u62df\u8def\u5f84
server.servlet.context-path=/lzy

#\u914d\u7f6e\u5bf9\u8c61
person.id=1
person.name=\u674e\u7ea2\u8299
person.hobby=\u65c5\u6e38,\u7f8e\u98df,\u97f3\u4e50
person.family.father=\u674e\u5ead\u91d1
person.family.mother=\u5218\u53d1\u6885
person.family.grandpa=\u4e07\u9f99\u82b3
person.famliy.grandma=\u5218\u4e07
person.family.son=\u674e\u6b46
person.family.daughter=\u674e\u9ece
person.pet.type=\u91d1\u6bdb
person.pet.name=\u745e\u745e

D:\java\JSP\PropertiesDemo\src\main\resources>

修改application.properties文件,汉字采用unicode编码形式
在这里插入图片描述
运行测试类,查看结果
在这里插入图片描述
从Spring容器里获取Pet类的实例并输出
查看Pet类的注解,有配置属性的注解@ConfigurationProperties(prefix = “person.pet”)

在测试类里添加测试方法testPet()
在这里插入图片描述
运行测试
在这里插入图片描述
去掉Pet类的配置属性的注解@ConfigurationProperties(prefix = “person.pet”)
在这里插入图片描述

运行测试方法testPet(),查看结果
在这里插入图片描述
大家可以看到,宠物对象的属性依然没有被注入,下面我们换一种属性注解的方式,采用@Value注解方式。
给Pet类的属性添加值注解@Value

在这里插入图片描述
再次运行测试方法testPet(),查看结果,出现错误
在这里插入图片描述

在这里插入图片描述

两种属性注解方式的对比
采用@ConfigurationProperties注解方式,必须要有set方法,才会自动为所注解的类的全部属性注入相应的值,包括简单类型和复杂类型(List、Map、Pet……)。
采用@Value注解方式,优点在于可以不要set方法,但是有两点不足:其一、需要一个一个地注入,显得麻烦;其二、对于复杂类型不能注入,比如Map、List、Pet等。

Application.yaml配置文件
1、备份application.properties文件
文件更名为application.back,即让此文件不起作用
在resoures目录里创建application.yaml文件
创建application.yaml文件
在这里插入图片描述

配置服务器属性
在这里插入图片描述
运行测试方法contextLoads(),查看结果
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值