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

网友投稿 550 2022-11-27

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

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

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

一、@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 javaIMDIK.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 licences;

@Value("#{${petshop.infos}}")

private Map infos;

@RequestMapping("/show")

public Object show() {

Map 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 licences;

private Map infos;

}

测试注入的结果:

package com.it.action;

imhttp://port 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 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 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 pets;

}

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

总结

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Sagit.Framework For IOS 自动布局教程:11、常用宏定义:frame坐标系、获取UI、UI取值、字体颜色、图片
下一篇:CYQ.Data+EasyUI开发:几个相关的问题CheckBox、Tree、TreeGrid
相关文章

 发表评论

暂时没有评论,来抢沙发吧~