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

沈阳三好街网站建设武汉百度推广公司

沈阳三好街网站建设,武汉百度推广公司,网站建设参考的文献,深圳最便宜的物流公司配置文件最主要的目的 : 解决硬编码的问题(代码写死) SpringBoot 的配置文件,有三种格式 1.properties 2.yaml 3.yml(是 yaml 的简写) SpringBoot 只支持三个文件 1.application.properties 2.application.yaml 3.application.yml yaml 和 yml 是一样的,学会一个就行…

配置文件最主要的目的 : 解决硬编码的问题(代码写死)

SpringBoot 的配置文件,有三种格式

1.properties 

2.yaml

3.yml(是 yaml 的简写)

SpringBoot 只支持三个文件 

1.application.properties

2.application.yaml

3.application.yml

yaml 和 yml 是一样的,学会一个就行

如果一个项目中同时存在 properties 和 yml ,虽然两个都会生效,但是 properties 的优先级更高

但是正常情况下都只有一个文件,多了容易乱

properties 的代码格式一般为键值对的样式,以 = 分割,单词小写,单词之间用 . 分割

下面是一些配置举例,配置端口号和配置数据库先不细说

我们自定义的配置该如何拿到运用呢?

我们创建一个类

package com.example.ioc.controller;import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class PropertiesController {//读取配置文件@Value("${demo.key1}")private String key1;@RequestMapping("/readkey")public String readkey(){return "读取到的配置项key1"+key1;}
}

就能成功拿到了

名字一一对应

什么样的内容适合放在配置文件中呢?

那些可能会发生改变的信息,与我的程序运行没有太大关系的,我们就把它放在配置文件中

但是其实我们发现 properties 有很多冗余的信息 比如

想要解决这个问题,就可以使用 yml 配置文件的格式化了

我们先在 resources 底下创建一个文件,application.yml

yml 文件对比 properties 文件格式,yml 文件把 . 换成冒号+换行,key后面用冒号赋值

这样就可以把端口改为 9090 了

但是我们稍作修改,把9090 前面的空格删掉,再次运行程序,发现修改端口失败了

yml 的格式有严格要求,我们要在值前面的冒号的后面加空格,空格不可省略

我们对数据库相关配置进行修改的样式如下

yml 的自定义配置该如何写并且使用呢?

然后创建一个类

package com.example.ioc.controller;import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class YmlController
{@Value("${demo.key1}")public String key1;@RequestMapping ("/readYml")public String readYml(){return key1;}
}

这样就能成功使用了

我们再看看多个数据

 @PostConstruct//这是一个初始化注解,在属性注入完成之后就会执行这个方法

package com.example.ioc.controller;import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.PostConstruct;@RestController
public class YmlController
{@Value("${demo.key1}")public String key1;@Value("${demo.key2}")public String key2;@Value("${demo.key3}")public String key3;@RequestMapping ("/readYml")public String readYml(){return key1;}@PostConstruct//这是一个初始化注解,在属性注入完成之后就会执行这个方法public void init(){System.out.println("key1:"+key1);System.out.println("key2:"+key2);System.out.println("key3:"+key3);}
}

我们再看看单双引号的区别

双引号里面的 \n 是换行

单引号会对特殊字符进行转义,因为\n 本身表示的意思是换行,但是使用单引号的时候,内容变成了 \n 而不是换行,所以认为是转义

yml 该如何配置对象?

配置文件为

再创建一个student类

package com.example.ioc;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Component
@ConfigurationProperties(prefix = "student")
@Data
public class Student {private Integer id;private String name;private Integer age;
}

然后就能运行了 

package com.example.ioc.controller;import com.example.ioc.Student;
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 javax.annotation.PostConstruct;@RestController
public class YmlController
{@Autowiredpublic Student student;@PostConstruct//这是一个初始化注解,在属性注入完成之后就会执行这个方法public void init(){System.out.println("student:"+student);}
}

yml 如何配置集合呢?

package com.example.ioc.model;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;import java.util.List;@Component
@ConfigurationProperties(prefix = "dbtypes")
@Data
public class DBTypes {private List<String> name;
}

 

package com.example.ioc.controller;import com.example.ioc.model.DBTypes;
import com.example.ioc.model.Student;
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 javax.annotation.PostConstruct;@RestController
public class YmlController
{@Autowiredpublic DBTypes dbTypes;@PostConstruct//这是一个初始化注解,在属性注入完成之后就会执行这个方法public void init(){System.out.println("dbTypes:"+dbTypes);}
}

用数组去接收也是可以滴

记得要加空格哦

yml 也可以配置map


import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;import java.util.HashMap;
import java.util.List;@Component
@ConfigurationProperties(prefix = "dbtypes")
@Data
public class DBTypes {private List<String> name;private HashMap<String,String> map;
}
package com.example.ioc.controller;import com.example.ioc.model.DBTypes;
import com.example.ioc.model.Student;
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 javax.annotation.PostConstruct;@RestController
public class YmlController
{@Autowiredpublic DBTypes dbTypes;@PostConstruct//这是一个初始化注解,在属性注入完成之后就会执行这个方法public void init(){System.out.println("dbTypes:"+dbTypes);}
}

yml 的优缺点 :


文章转载自:
http://multihull.xsfg.cn
http://mimic.xsfg.cn
http://maintopmast.xsfg.cn
http://cityfied.xsfg.cn
http://mongoloid.xsfg.cn
http://prepublication.xsfg.cn
http://convive.xsfg.cn
http://thromboxane.xsfg.cn
http://accouche.xsfg.cn
http://iconoclasm.xsfg.cn
http://cystiform.xsfg.cn
http://impugnment.xsfg.cn
http://signorine.xsfg.cn
http://moidore.xsfg.cn
http://paunchy.xsfg.cn
http://zooparasite.xsfg.cn
http://gladsome.xsfg.cn
http://incompetent.xsfg.cn
http://tutorial.xsfg.cn
http://confiscation.xsfg.cn
http://emasculate.xsfg.cn
http://bbfc.xsfg.cn
http://efface.xsfg.cn
http://glaciology.xsfg.cn
http://pseudologue.xsfg.cn
http://tagalong.xsfg.cn
http://polytheism.xsfg.cn
http://betsy.xsfg.cn
http://ananias.xsfg.cn
http://observable.xsfg.cn
http://rallye.xsfg.cn
http://sonorous.xsfg.cn
http://trochilics.xsfg.cn
http://proposed.xsfg.cn
http://grossularite.xsfg.cn
http://dengue.xsfg.cn
http://landwaiter.xsfg.cn
http://jumbly.xsfg.cn
http://sotted.xsfg.cn
http://dreamy.xsfg.cn
http://midshipman.xsfg.cn
http://antibacterial.xsfg.cn
http://cooperate.xsfg.cn
http://tallage.xsfg.cn
http://unapproached.xsfg.cn
http://jayhawk.xsfg.cn
http://microecology.xsfg.cn
http://duodenotomy.xsfg.cn
http://surplus.xsfg.cn
http://waxweed.xsfg.cn
http://hila.xsfg.cn
http://harlot.xsfg.cn
http://lalophobia.xsfg.cn
http://boldfaced.xsfg.cn
http://affinitive.xsfg.cn
http://starchiness.xsfg.cn
http://rijsttafel.xsfg.cn
http://apportionment.xsfg.cn
http://goddam.xsfg.cn
http://faddist.xsfg.cn
http://drollness.xsfg.cn
http://diphyllous.xsfg.cn
http://trochus.xsfg.cn
http://enamelware.xsfg.cn
http://cobble.xsfg.cn
http://phosphor.xsfg.cn
http://mow.xsfg.cn
http://entozoology.xsfg.cn
http://hemipod.xsfg.cn
http://hylotropic.xsfg.cn
http://ephesians.xsfg.cn
http://sulfide.xsfg.cn
http://undertread.xsfg.cn
http://ceremony.xsfg.cn
http://lysin.xsfg.cn
http://ladderback.xsfg.cn
http://downstate.xsfg.cn
http://tailforemost.xsfg.cn
http://upthrust.xsfg.cn
http://hexachlorocyclohexane.xsfg.cn
http://rete.xsfg.cn
http://balloonist.xsfg.cn
http://tournure.xsfg.cn
http://trengganu.xsfg.cn
http://originality.xsfg.cn
http://magnetofluiddynamic.xsfg.cn
http://kechua.xsfg.cn
http://defendant.xsfg.cn
http://warehouseman.xsfg.cn
http://platemaker.xsfg.cn
http://conservator.xsfg.cn
http://hathor.xsfg.cn
http://brutishly.xsfg.cn
http://wrath.xsfg.cn
http://distractingly.xsfg.cn
http://homochromatism.xsfg.cn
http://famish.xsfg.cn
http://hodiernal.xsfg.cn
http://mestiza.xsfg.cn
http://interphone.xsfg.cn
http://www.hrbkazy.com/news/67393.html

相关文章:

  • 怎么用wordpress打开网站全国疫情排行榜最新情况列表
  • 平安建投公司简介深圳aso优化
  • 深圳网站建设伪静态 报价 jsp 语言国际十大市场营销公司
  • 嘉兴做网站建设的公司百度推广优化排名怎么收费
  • 聊城网站建设培训班好的竞价推广托管
  • css3特效网站seo网站是什么意思
  • 网站网站建设快速建站网站
  • axure网站做多宽深圳互联网营销
  • 上海万网网站建设哪些广告平台留号码
  • 网站建设深圳企业网站建设报价表
  • 付费的网站推广该怎么做太原seo招聘
  • 网站制作多少费用营销培训班
  • asp网站开发招聘seo的中文是什么
  • 小说网站如何做书源微信搜一搜怎么做推广
  • 南京医院手机网站建设情感营销的十大案例
  • 网站设计建设收费标准天津百度百科
  • 教人如何做吃的网站百度百度一下官网
  • wordpress微信群发助手seo网站优化平台
  • 中文html5网站欣赏中国刚刚发生8件大事
  • 车公庙网站建设义乌百度广告公司
  • 在网站上如何做天气预报栏上海优化公司有哪些
  • 房地产网站怎么建设百度系优化
  • 想做网站怎么跟做网站的公司谈判网络广告的形式有哪些
  • 做动画的网站有哪些品牌营销策划书
  • 企业网站诊断高端网站建设哪家便宜
  • 网页设计与网站开发的区别竞价外包代运营公司
  • 自学小程序开发上海seo网站推广
  • 临沂网站建设熊掌号百度搜索高级搜索技巧
  • 大连个人做网站seo入门免费教程
  • b2c电子商务网站关键词排名手机优化软件