当前位置: 首页 > news >正文

西安网站建设公司西安网络公司互联网营销师培训学校

西安网站建设公司西安网络公司,互联网营销师培训学校,4399网页版,世界疫情最新数据排名表哈喽,大家好,我是有勇气的牛排(全网同名)🐮🐮🐮 有问题的小伙伴欢迎在文末评论,点赞、收藏是对我最大的支持!!!。 1 使用 Value 注解 /** Auth…

哈喽,大家好,我是有勇气的牛排(全网同名)🐮🐮🐮

有问题的小伙伴欢迎在文末评论,点赞、收藏是对我最大的支持!!!。

1 使用 @Value 注解

/** @Author  : 有勇气的牛排* @FileName: ReadProperties.java* desc     :* */package com.couragesteak.service;import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class ReadProperties {@Value("${cs.name}")private String name;@Value("${cs.star}")private String star;// http://127.0.0.1:8080/getProperties@RequestMapping("/getProperties")public String getProperties() {return name + ":" + star;}
}

2 使用 ConfigurationProperties

@ConfigurationProperties 加载配置通过 BeanPostProcessor实现,其对应的Bean的后置处理器为 ConfigurationPropertiesBindingPostProcessor,也就是在bean被实例化后,会调用后置处理器,递归的查找属性,通过反射机制注入值,因此需要提供getter和setter方法。

此外,针对此种属性注入的方式,SpringBoot支持 Relaxed Bingding,即秩序保证配置文件的属性和setter方法名相同即可。

注意事项

(1) 属性必须要有getter、setter方法
(2) 如果属性类型是集合,需要确保集合是不可变的。
(3) 如果使用Lombok自动生成 getter/setter方法,一定不要生成对应的任何构造函数,因为Spring IOC容器会自动使用它来实例化对象。
(4) 使用JavaBean属性绑定的方式只针对 标准Java Bean属性,不支持对静态属性的绑定。

配置文件

application.yml

cs:name: csstar: 9

2.1 Maven依赖

<!--导入配置文件处理器,配置文件进行绑定就会有提示-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional>
</dependency>

2.2 实体类

/** @Author  : 有勇气的牛排* @FileName: UserEntity.java* desc     :* */package com.couragesteak.entity;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Component
@ConfigurationProperties(prefix = "cs")
public class UserEntity {private String name;private String star;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getstar() {return star;}public void setstar(String star) {this.star = star;}@Overridepublic String toString() {return "UserEntity{" +"name='" + name + '\'' +", star='" + star + '\'' +'}';}
}

2.3 后端

/** @Author  : 有勇气的牛排* @FileName: ReadProperties_ConfigurationProperties.java* desc     : ConfigurationProperties 配置文件读取* */package com.couragesteak.service;import com.couragesteak.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class ReadProperties_ConfigurationProperties {@Autowiredprivate UserEntity userEntity;//@RequestMapping("/getInfo")public String getInfo(String userName, Integer age){System.out.println("666");return userEntity.toString();}
}

注意事项

Spring考虑到带有注释@ConfigurationProperties的类可能不适合扫描(比如:我正在开发自己的自动配置或希望有条件地启用它们),所以Spring并不会自动扫描带有注释@ConfigurationProperties的类。

在这些情况下,推荐使用@EnableConfigurationProperties注解指定要处理的类型列表(即:将@ConfigurationProperties注释的类加到Spring IOC容器中)。一般通过将@EnableConfigurationProperties加在@Configuration类上完成。

3 @Value和@ConfigurationProperties的区别

(1) @ConfigurationProperties注解支持属性文件和javabean映射;而@Value支持spel表达式。

(2) @ConfigurationProperties注解支持全量的属性,宽松绑定方式;而@Vavlue只推荐使用标准的 kebab-case方式(仅使用小写字母和-),例如@Value("{demo.item-price}")可以提取demo.item-pricedemo-itemPrice

(3) 对于多个属性映射,并且属性尝尝被复用时,推荐使用@ConfigurationProperties;只读取单个属性使用@Value更加方便。

参考地址:

  • 余胜军
  • https://developer.aliyun.com/article/1058227
  • https://www.couragesteak.com/article/292
http://www.hkea.cn/news/216349/

相关文章:

  • 建站城企业邮箱怎么开通注册
  • html做动态网站cms
  • 一个网站建设需要多少钱百度seo排名优化公司
  • 网站做app的软件友博国际个人中心登录
  • 做网站用什么代码编写可口可乐软文营销案例
  • 宜昌网站建设哪家好厦门百度广告开户
  • 网站做二级域名外链
  • 网站建设服务费属于哪个大类电商seo搜索优化
  • 12380网站建设情况的报告网络seo首页
  • 个人如何在百度上做广告网站seo什么意思
  • java做网站编程合肥seo快排扣费
  • 做律师网站公司google play下载
  • 网站怎么做详情页北京网站制作建设公司
  • 广告网站模板下载不了东莞排名优化团队
  • 网站建设人员培训纲要河北seo网络推广
  • jsp网站开发实例视频教程各大网站的网址
  • 手机网站设计要素推广竞价
  • 久久医药网seo推广培训费用
  • 网站做301顶级域名需要绑定网站排名掉了怎么恢复
  • wordpress app 源码合肥seo整站优化网站
  • 建立网站基本步骤安仁网络推广
  • 网页建设方案怎么写网站seo优化心得
  • 还没有做网站可以先备案域名吗seo怎么提升关键词的排名
  • 做网站原型图软件优化设计七年级下册语文答案
  • 2023年舆情分析报告seo优化宣传
  • 武汉网站建设 熊掌号最佳磁力引擎吧
  • 教育平台网站开发品牌运营
  • 91人才网赣州招聘网安卓优化大师app下载安装
  • 合肥网页模板建站营业推广策划
  • 网站做301根目录在哪教育培训机构平台