SpringBoot属性注入

SpringBoot属性注入
1、 通过application.properties(.yml/.yaml)和@Value进行注入
@Value的值有以下三大类:
(1)、@Value("${property:default_value}")
$注入的是外部配置文件对应的property,使用“:”对未配置或值为空的属性设置默认值。
(2)、@Value("#{obj.property?:default_value}")
#注入的是SpEL表达式对应的内容,使用“ ?: ”对未配置或值为空的表达式设置默认值。
(3)、直接把值注入 @Value("字符串类型的值")
在注入Map时 需要使用 #{${}} 结合使用
①、application-dev.properties配置文件:

# 属性注入测试
# 字符串
string=这是字符串
# 数字 整型 浮点型
intv=20
doublev=99.9
# 数组/List
string-arr=元素1,元素2,3
# map
mapsv= {key1: 'value1', key2: 'value2'}
#person
person.name=张三

application-test.yaml配置文件:

# 属性注入测试
# 字符串
string: 这是字符串
# 数字 整型 浮点型
intv: 20
doublev: 99.9
# 数组/List
string-arr: 元素1,元素2,3
# map 一定要用""把map所对应的value包起来,要不然解析会失败
mapsv: "{key1: 'value1', key2: 'value2'}"
#person
person:
name: 张三

②、在HelloController里通过@Value注解注入

package com.gx.springbootdemoinitializr.controller;
import com.gx.springbootdemoinitializr.vo.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@RestController
public class HelloController // 字符串值
@Value("${string}")
private String strValue;
// 数值 整型和浮点型 需要使用 #{${}}结合使用
@Value("#{${intv}}")
short shortValue;
@Value("#{${intv}}")
int intValue;
@Value("#{${doublev}}")
float floatValue;
@Value("#{${doublev}}")
double doubleValue;
//配置中的数组可以注入为String[]或List<String>
@Value("${string-arr}")
String[] strArr;
@Value("${string-arr}")
List<String> strList;
// map 需要使用 #{${}}结合使用
@Value("#{${mapsv}}")
Map<String, Object> mapValue;
// 指定默认值
@Value("${default-value:默认值}")
String strDefaultValue;
//直接把值注入
@Value("直接把指定的值注入"String noProperty;
@Value("#{1234}")
int noPropertyInt;
@Value("${person.name}")
private String personName;
@RequestMapping("/hello"public String hello() {System.out.println(strValue);//字符串
System.out.println(shortValue);//整型 short
System.out.println(intValue);//整型 int
System.out.println(floatValue);//浮点数 float
System.out.println(doubleValue);//浮点数 double
//数组
for (String s : strArr) {
System.out.println(s);
}
System.out.println(strList);//List
System.out.println(mapValue);//map
//默认值
System.out.println(strDefaultValue);
//直接把值注入
System.out.println(noProperty);
System.out.println(noPropertyInt);
System.out.println(personName);
return "Hello SpringBoot! (Create by IDEA Spring Initializr)!spring-boot-devtools !";
}
}

@Value在使用中也有诸多的不便,注入时需要一个一个的绑定,比较繁琐,适合少量注入。不支持复杂类型封装。
2、通过@ConfigurationProperties在实体类上注解注入属性值
@ConfigurationProperties@Value不同,适合于批量注入配置文件中的属性,并且支持复杂类型封装。
①、在配置文件中配置
1)、properties的写法

#person
person.name=张三
person.age=32
person.birth=1990/11/01
person.stage=true
person.maps.name=jack
person.maps.age=16
person.lists=a,b,c,d,e,f
person.child.name=露西
person.child.age=11

2)、yaml的写法:

#person
person:
  name: 张三
   age: 32
 birth: 1990/11/01
 stage: true
  maps:
    name: jack
     age: 16
 lists: a,b,c,d,e,f
 child:
    name: 露西
     age: 11

解决使用@ConfigurationProperties后IDEA爆红
使用@ConfigurationProperties后,IDEA会在编辑器顶部提示Spring Boot Configuration Annotation Processor not configured
在这里插入图片描述
可以添加配置文件处理器依赖,可以解决IDEA爆红,并且以后编写配置时就有提示了

<!--导入配置文件处理器,配置文件进行绑定就会有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,属性注入可以通过不同的方式实现。一种常见的方式是使用`@ConfigurationProperties`注解。通过将该注解应用于一个类上,可以将`application.properties`或`application.yml`文件中定义的属性注入Java类中。这种注入是通过Java属性的setter方法进行的。 另外一种方式是通过在`@Bean`方法上使用`@ConfigurationProperties`注解。在这种情况下,Spring Boot会自动调用该`@Bean`方法,并将相关属性通过setter方法注入到对应的Bean中。需要注意的是,被注入属性的类必须具有对应属性的setter方法。 为了观察被注入属性,你可以通过访问`http://localhost:8080/source/show`来查看。这个URL将展示被注入属性信息。另外,还可以使用`@ConfigurationProperties`注解进行批量注入属性,以方便地注入多个属性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [SpringBoot属性注入](https://blog.csdn.net/Nicholas_GUB/article/details/120997989)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [SpringBoot 属性注入](https://blog.csdn.net/2301_77025309/article/details/130994152)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值