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

海口免费做网站域名查询

海口免费做网站,域名查询,2023年山东疫情最新消息,wordpress柒比贰文章目录 1.通过 value 读取比较简单的配置信息2.通过ConfigurationProperties读取并与 bean 绑定3.通过ConfigurationProperties读取并校验4. PropertySource 读取指定 properties 文件5.题外话:Spring加载配置文件的优先级 很多时候我们需要将一些常用的配置信息比如阿里云os…

文章目录

      • 1.通过 `@value` 读取比较简单的配置信息
      • 2.通过`@ConfigurationProperties`读取并与 bean 绑定
      • 3.通过`@ConfigurationProperties`读取并校验
      • 4. '@PropertySource' 读取指定 properties 文件
      • 5.题外话:Spring加载配置文件的优先级

很多时候我们需要将一些常用的配置信息比如阿里云oss配置、发送短信的相关信息配置等等放到配置文件中。

下面我们来看一下 Spring 为我们提供了哪些方式帮助我们从配置文件中读取这些配置信息。

application.yml 内容如下:

wuhan2020: 2020年初武汉爆发了新型冠状病毒,疫情严重,但是,我相信一切都会过去!武汉加油!中国加油!my-profile:name: Guide哥email: koushuangbwcx@163.comlibrary:location: 湖北武汉加油中国加油books:- name: 天才基本法description: 二十二岁的林朝夕在父亲确诊阿尔茨海默病这天,得知自己暗恋多年的校园男神裴之即将出国深造的消息——对方考取的学校,恰是父亲当年为她放弃的那所。- name: 时间的秩序description: 为什么我们记得过去,而非未来?时间“流逝”意味着什么?是我们存在于时间之内,还是时间存在于我们之中?卡洛·罗韦利用诗意的文字,邀请我们思考这一亘古难题——时间的本质。- name: 了不起的我description: 如何养成一个新习惯?如何让心智变得更成熟?如何拥有高质量的关系? 如何走出人生的艰难时刻?

1.通过 @value 读取比较简单的配置信息

使用 @Value("${property}") 读取比较简单的配置信息:

@Value("${wuhan2020}")
String wuhan2020;

需要注意的是 @value这种方式是不被推荐的,Spring 比较建议的是下面几种读取配置信息的方式。

2.通过@ConfigurationProperties读取并与 bean 绑定

LibraryProperties 类上加了 @Component 注解,我们可以像使用普通 bean 一样将其注入到类中使用。

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;import java.util.List;@Component
@ConfigurationProperties(prefix = "library")
@Setter
@Getter
@ToString
class LibraryProperties {private String location;private List<Book> books;@Setter@Getter@ToStringstatic class Book {String name;String description;}
}

这个时候你就可以像使用普通 bean 一样,将其注入到类中使用:

package cn.javaguide.readconfigproperties;import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** @author shuang.kou*/
@SpringBootApplication
public class ReadConfigPropertiesApplication implements InitializingBean {private final LibraryProperties library;public ReadConfigPropertiesApplication(LibraryProperties library) {this.library = library;}public static void main(String[] args) {SpringApplication.run(ReadConfigPropertiesApplication.class, args);}@Overridepublic void afterPropertiesSet() {System.out.println(library.getLocation());System.out.println(library.getBooks());    }
}

控制台输出:

湖北武汉加油中国加油
[LibraryProperties.Book(name=天才基本法, description........]

3.通过@ConfigurationProperties读取并校验

我们先将application.yml修改为如下内容,明显看出这不是一个正确的 email 格式:

my-profile:name: Guide哥email: koushuangbwcx@

ProfileProperties 类没有加 @Component 注解。我们在我们要使用ProfileProperties 的地方使用@EnableConfigurationProperties注册我们的配置bean:

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;/**
* @author shuang.kou
*/
@Getter
@Setter
@ToString
@ConfigurationProperties("my-profile")
@Validated
public class ProfileProperties {@NotEmptyprivate String name;@Email@NotEmptyprivate String email;//配置文件中没有读取到的话就用默认值private Boolean handsome = Boolean.TRUE;}

具体使用:

package cn.javaguide.readconfigproperties;import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;/*** @author shuang.kou*/
@SpringBootApplication
@EnableConfigurationProperties(ProfileProperties.class)
public class ReadConfigPropertiesApplication implements InitializingBean {private final ProfileProperties profileProperties;public ReadConfigPropertiesApplication(ProfileProperties profileProperties) {this.profileProperties = profileProperties;}public static void main(String[] args) {SpringApplication.run(ReadConfigPropertiesApplication.class, args);}@Overridepublic void afterPropertiesSet() {System.out.println(profileProperties.toString());}
}

因为我们的邮箱格式不正确,所以程序运行的时候就报错,根本运行不起来,保证了数据类型的安全性:

Binding to target org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'my-profile' to cn.javaguide.readconfigproperties.ProfileProperties failed:Property: my-profile.emailValue: koushuangbwcx@Origin: class path resource [application.yml]:5:10Reason: must be a well-formed email address

我们把邮箱测试改为正确的之后再运行,控制台就能成功打印出读取到的信息:

ProfileProperties(name=Guide哥, email=koushuangbwcx@163.com, handsome=true)

4. ‘@PropertySource’ 读取指定 properties 文件

import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;@Component
@PropertySource("classpath:website.properties")
@Getter
@Setter
class WebSite {@Value("${url}")private String url;
}

使用:

@Autowired
private WebSite webSite;System.out.println(webSite.getUrl());//https://javaguide.cn/

5.题外话:Spring加载配置文件的优先级

Spring 读取配置文件也是有优先级的,直接上图:

更对内容请查看官方文档:https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config

http://www.khdw.cn/news/39601.html

相关文章:

  • wordpress导购主题免费海口网站关键词优化
  • 哈尔滨网站建设哪家好淘宝补流量平台
  • 网站平台建设需要注意的是seo的特点是什么
  • android+wordpress哈尔滨seo整站优化
  • 网站建设费用怎么记账500个游戏推广群
  • 北京响应式网站开发郑州网络营销与网站推广
  • 影响搜索排名的核心因素有哪些?seo编辑的工作内容
  • 做网站设计最好的公司b站引流推广网站
  • 色情网站弹出窗口去掉百度怎么优化排名
  • 河北特定网站建设推荐纵横seo
  • asp网站上哪做百度地图打车客服人工电话
  • 网站备案升级如何做网站优化seo
  • 南宁网站制作-中国互联佛山做网站的公司哪家好
  • 白银做网站的董事快推广app下载
  • 松岗做网站联系电话西安网站开发制作公司
  • php网站做退出的代码站外推广怎么做
  • 人才网站建站短视频代运营公司
  • 网站模板出售中国搜索
  • 上海做公司网站的公司石家庄最新消息今天
  • 编辑网站内容有没有批量办法百度推广登陆
  • 丹阳市最新疫情杭州优化公司多少钱
  • 找人做网站需要问哪些问题南宁网络推广培训机构
  • 郑州易站通网站公司成都网站建设公司
  • 域名审核怎么做返利网站论坛seo网站
  • 建设部网站首页督办案件优化人员是什么意思
  • 个人网站作品下载网络优化工资一般多少
  • 本科毕业设计代做网站郴州网络推广公司排名
  • 18互联网站做网站程序seo关键词是什么意思
  • php网站安装好后后台无法登陆提示是500是怎么回事?百度小说风云榜
  • 网站head蓝色色调免费psd背景青岛做网络推广的公司有哪些