iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringBoot属性注入的多种方式实例
  • 362
分享到

SpringBoot属性注入的多种方式实例

2024-04-02 19:04:59 362人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录一、@Value注解注入属性 二、@ConfigurationProperties注解批量注入属性 三、注入实体对象 四、自定义文件注入 总结一、@Value注解注入属性 Sp

一、@Value注解注入属性

SpringBoot默认可以将application.properties文件或application.yml文件中定义的属性值注入到java类中,这种注入实际上是通过java类属性的setter方法进行的。

例:将application.yml中的以下属性注入到类中:

## 自定义属性
petshop:
  name: 睿芽宠物
  introduce: 种类齐全,安全可靠
  licences: 1、上市许可证,2、疫苗许可证
  infos: "{'phone':'36xx102','address':'xx省xx市'}"

使用@Value注解可以将application.yml中的属性注入,@Value注解使用${属性名}的方式来声明要注入的属性,如果要注入的属性为Map集合,则需要结合Spel表达式进行处理。


package com.it.action;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.WEB.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
@RequestMapping("/source")
public class SourceAction {
    @Value("${petshop.name}")
    private String name;
    @Value("${petshop.introduce}")
    private String introduce;
    @Value("${petshop.licences}")
    private List<String> licences;
    @Value("#{${petshop.infos}}")
    private Map<String, String> infos;

    @RequestMapping("/show")
    public Object show() {
        Map<String, Object> map = new LinkedHashMap();
        map.put("name", name);
        map.put("introduce", introduce);
        map.put("licences", licences);
        map.put("infos", infos);
        return map;
    }
}

访问Http://localhost:8080/source/show观察被注入的属性:

二、@ConfigurationProperties注解批量注入属性

@ConfigurationProperties注解用于注入有着相同前缀的属性,注入的方式也是通过java类的setter方法来完成,但是这种方式缺少了@Value注解的灵活性,也无法结合spel语言进行处理。

例:将application.yml中的以下属性注入到类中:

## 自定义属性
petshop:
  name: 睿芽宠物
  introduce: 种类齐全,安全可靠
  licences: 上市许可证,疫苗许可证
  infos:
    - phone: 36xx102
    - address: xx省xx市

新建PetShop类并注入属性:


package com.it.vo;

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

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

@Data
@Component
@ConfigurationProperties(prefix = "petshop")
public class PetShop {
    private String name;
    private String introduce;
    private List<String> licences;
    private Map<String, String> infos;
}

测试注入的结果:


package com.it.action;

import com.it.vo.PetShop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/source")
public class SourceAction {
    @Autowired
    private PetShop petShop;

    @RequestMapping("/show")
    public Object show() {
        return petShop;
    }
}

三、注入实体对象

使用@ConfigurationProperties注解可以将关联的对象一同注入。

修改application.yml文件:

## 自定义属性
petshop:
  name: 睿芽宠物
  introduce: 种类齐全,安全可靠
  shopInfo:
    phone: 36xx102
    address: xx省xx市
    licences: 上市许可证,疫苗许可证
  pets:
    - pet:
      name: 金毛
      price: 3365.21
    - pet:
      name: 巴哥
      price: 2136.10

新建三个java类,并设置好引用关系:


@Data
public class PetShopInfo {
    private String phone;
    private String address;
    private List<String> licences;
}

@Data
public class Pet {
    private String name;
    private double price;
}

@Data
@Component
@ConfigurationProperties(prefix = "petshop")
public class PetShop {
    private String name;
    private String introduce;
    private PetShopInfo shopInfo;
    private List<Pet> pets;
}

测试注入结果:


@RestController
@RequestMapping("/source")
public class SourceAction {
    @Autowired
    private PetShop petShop;

    @RequestMapping("/show")
    public Object show() {
        return petShop;
    }
}

四、自定义文件注入

在resource目录下新建petshop/petshop.properties文件,将application.yml中的属性转换为properties中的key-value格式:

## 自定义属性
petshop.name=睿芽宠物
petshop.introduce=种类齐全,安全可靠

petshop.shopInfo.phone=36xx102
petshop.shopInfo.address=xx省xx市
petshop.shopInfo.licences=上市许可证,疫苗许可证

petshop.pets[0].name=金毛
petshop.pets[0].price=3365.21

petshop.pets[1].name=巴哥
petshop.pets[1].price=2136.10

修改PetShop类,添加@PropertySource注解导入properties文件


@Data
@Component
@PropertySource(value = "classpath:petshop/petshop.properties", encoding = "UTF-8")
@ConfigurationProperties(prefix = "petshop")
public class PetShop {
    private String name;
    private String introduce;
    private PetShopInfo shopInfo;
    private List<Pet> pets;
}

访问http://localhost:8080/source/show发现可以得到与上例相同的结果。

总结

到此这篇关于SpringBoot属性注入的多种方式的文章就介绍到这了,更多相关SpringBoot属性注入内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: SpringBoot属性注入的多种方式实例

本文链接: https://www.lsjlt.com/news/155938.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作