SpringBoot属性注入

x33g5p2x  于2021-12-06 转载在 Spring  
字(3.9k)|赞(0)|评价(0)|浏览(297)

一、@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发现可以得到与上例相同的结果。

相关文章

微信公众号

最新文章

更多