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

婚庆网站建设总结公司企业网站程序下载

婚庆网站建设总结,公司企业网站程序下载,核酸检测赚了七十亿,网站速度的重要性文章目录 前言1.多环境配置application.propertiesapplication.yaml 2.常用配置3.配置读取4.自定义配置 前言 在涉及项目开发时#xff0c;通常我们会灵活地把一些配置项集中在一起#xff0c;如果你的项目不是很大的情况下#xff0c;那么通过配置文件集中不失为一个很好的… 文章目录 前言1.多环境配置application.propertiesapplication.yaml 2.常用配置3.配置读取4.自定义配置 前言 在涉及项目开发时通常我们会灵活地把一些配置项集中在一起如果你的项目不是很大的情况下那么通过配置文件集中不失为一个很好的解决方案。 在 Spring Boot 中我们可以方便地通过读取 appliction.properties/application.yaml 格式的配置文件进而注入我们的项目中。 1.多环境配置 在日常开发中我们的环境根据不同的阶段会有一定不同总的可以分为dev、test、prod举个简单的例子不同的环境中如 port 可能是不同的。 上文中提到配置文件多以 appliction.properties/application.yaml 这两种格式为主下面分别就这两种格式对多环境的配置做个说明。 application.properties 在这种格式中主配置文件是 application.properties对于不同环境的配置通常我们会命名为 application-xxx.properties这里的 xxx 可以是 dev、test、prod 中一种比如我们的 主配置文件(appliction.properties) 内容如下 properties server.portspring.profiles.activedev这样配置读取时就会去 application-dev.properties 中读取相关的配置其他同理。 application.yaml 熟悉 yaml 的小伙伴对其格式肯定不陌生通常都是同级内容对齐分级项通过另启一行且通常需要固定的空格缩进一般是2个空格这里不多说格式问题自行搜索。 我们假定主配置文件是 application.yaml其他环境的配置文件是application-dev.yaml/application-test.yaml/application-prod.yaml 。 这里我们看看在看看 yaml 主配置文件中怎么配置 server:port: 8080spring:profiles:active: dev 2.常用配置 通常我们的配置应该包含如下选项 server运行配置如端口ip是否SSL超时时间多线程等server的介绍信息配置日志配置信息又或者日志部分单独配置数据库的配置信息缓存的配置信息 比如我们的配置项可以是以下信息 # server server:port: 8000tomcat:threads:max: 10min-spare: 3uri-encoding: UTF-8# self define app:name: springDemodesc: a-spring-boot-appversion: 1.0.0author: Alice-Knight# logging logging:file:name: app.logpath: ../logslogback:rollingpolicy:max-file-size: 5MBmax-history: 15pattern:dateformat: yyyy-mm-ddTHH:MM:ss.SSSXXX# database cache es spring:datasource:url: jdbc:mysql://localhost:3306/demousername: rootpassword: 123456driver-class-name: com.mysql.jdbc.Driverdata:redis:database: 0connect-timeout: 120port: 6379host: 0.0.0.0jedis:pool:enabled: truemax-active: 10min-idle: 2elasticsearch:uris:- http://localhost:9200username: adminpassword: 123456connection-timeout: 120s3.配置读取 Value(${field})通过注解 Value 解析配置中的字段新建个控制器类主要功能就是返回 app 的 info package com.example.demo;import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;import java.util.HashMap; import java.util.Map;RestController public class RestfulController {Value(${app.version})private String version;Value(${app.author})private String author;Value(${app.desc})private String desc;GetMapping(value /appInfo)public Object getAppInfo() {MapString, String info new HashMap();info.put(version, version);info.put(author, author);info.put(desc, desc);return info;} }测试 从结果中可以看到返回的响应体包含我们配置文件中的字段信息。 Component ConfigurationProperties AutoWired这里我们用到自动装配注解。首先实现对象类就是一个 Bean, 然后类加上注解 Component ConfigurationProperties(prefix app)其次在使用到的控制类中设置变量加上注解 AutoWired具体实现如下 AppInfo.java package com.example.demo;import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;Component ConfigurationProperties(prefix app) public class AppInfo {private String author;private String desc;private String version;public String getAuthor() {return author;}public void setAuthor(String author) {this.author author;}public String getDesc() {return desc;}public void setDesc(String desc) {this.desc desc;}public String getVersion() {return version;}public void setVersion(String version) {this.version version;} }AppInfoController.java package com.example.demo;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;import java.util.HashMap; import java.util.Map;RestController public class AppInfoController {Autowiredprivate AppInfo appInfo;GetMapping(value /v2/appInfo)public Object getAppInfoV2() {MapString, String info new HashMap();info.put(version, appInfo.getVersion());info.put(author, appInfo.getAuthor());info.put(desc, appInfo.getDesc());return info;} }测试结果 从结果中可以看到采用该方法也可以实现配置项的读取。 4.自定义配置 比如我们在配置文件中定义了这个 app 的name、开发者、版本号等即如下 app:name: demoauthor: david-knightversion: 1.0.0 ...这个自定义的内容我们又怎么获取呢 先写个 Bean 来接收 app 的字段 package com.example.springbootdemo2.param;import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;Component ConfigurationProperties(prefix app) public class AppInfo {private String name;private String desc;private String version;public String getName() {return name;}public void setName(String name) {this.name name;}public String getDesc() {return desc;}public void setDesc(String desc) {this.desc desc;}public String getVersion() {return version;}public void setVersion(String version) {this.version version;} }然后在 控制器类中 自动装配 Object package com.example.springbootdemo2.controller;import com.example.springbootdemo2.param.AppInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import java.util.HashMap; import java.util.Map;RestController RequestMapping(value /app) public class AppInfoController {Autowiredprivate AppInfo appinfo;GetMapping(value /info)public Object getAppInfo() {MapString, Object info new HashMap();info.put(name, appinfo.getName());info.put(desc, appinfo.getDesc());info.put(version, appinfo.getVersion());return info;}}上面添加注解时提示有点小问题根据官方建议在 pom.xml 中添加 配置依赖 !-- 配置处理 -- dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-configuration-processor/artifactIdoptionaltrue/optional /dependency看看测试效果 从结果中可以看到配置项已经成功读取到。
http://www.zqtcl.cn/news/799629/

相关文章:

  • 网站建设开发成本设计素材库
  • wordpress获取站点链接wordpress 滑动验证
  • 怎么把网站上的通栏手机公司网站建设
  • 微山县建设局官方网站wordpress 内容换行
  • 网站选择空间ps个人主页设计
  • 河北网站seo外包网站嵌入百度地图
  • 公司怎么开网站WordPress有哪些工具
  • 一流专业建设网站原平新闻头条最新消息
  • 网站开发文档模板 开源北京保障房建设项目网站
  • 营销型网站分类网站关键词如何快速上首页
  • 帝国和WordPress比较wordpress文章页标题优化
  • 宁晋网站建设温岭新站seo
  • 大学科研项目做网站成都免费建站模板
  • 兰州网站开发企业在微信公众号发布wordpress
  • 网站信息化建设总体情况网站建设介绍ppt模板下载
  • 广州 建网站asp.net.网站开发
  • 装修网站模板国家正规现货交易平台
  • 福州高端网站制作网站建设项目单子来源
  • 网站制作的行业广州网站推广方案
  • 网站主域名建设通怎么样
  • 网站是如何建立的广告设计与制作工作内容
  • 网站优化课程培训公司取名生成器免费
  • 如何设立网站做外国网站买域名
  • 惠州网站建设公司排名聊城专业网站设计公司
  • 网站建设龙岗电子商务有限公司官网
  • 分栏型网站服装设计网站模板
  • 建设网站备案与不备案区别企业网站怎么做排名
  • php mysql的网站开发html网站制作答辩ppt
  • 网站制作有名 乐云践新专家网页制作公司需要什么资质
  • 织梦怎么用框架实现在浏览器的地址栏只显示网站的域名而不显示出文件名电脑网站模板