义乌网站建设方案详细,推广策略包括哪些方面,温州网站建设seo,做小程序怎么赚钱springboot提供了两种配置信息的文件格式#xff0c;application.properties和application.yml#xff0c;基于直接明了#xff0c;使用方便和高效的前提下下面的配置均采用yml格式配置#xff0c;
注意
yml采用缩减方式来排列键后面紧跟冒号#xff0c;然后空格#x…springboot提供了两种配置信息的文件格式application.properties和application.yml基于直接明了使用方便和高效的前提下下面的配置均采用yml格式配置
注意
yml采用缩减方式来排列键后面紧跟冒号然后空格最后是值注意里面用到爱好数字的配置信息每个数组值前面的-横杠和值之间有一个空格
properties也yml/ymal相互转换
在线yaml转properties-在线properties转yaml-ToYaml.com在线yaml转properties工具 - toyamlhttps://www.toyaml.com/
配置信息的分类三种
具体可以查看官网
Spring Boot Reference Documentationhttps://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#legal
springboot标准的配置
例如web服务的端口和上下文路径
server:port: 9999servlet:context-path: /yuanma
其他第三方配置信息
例如集成redis
spring:data:redis:host: localhostport: 6379database: 0username: userpassword: secret自定义配置信息(两种获取信息的方式)
ValueEL表达式
例如下面配置了邮件信息
#邮件信息
mail:from: 123qq.comto: 456qq.comserver: smtp.qq.comport: 8888
下面用ValueEL表达式配置
package com.burns.yuanma.admin.pojo;import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;Component
Data
public class MailProperties {/*** 发件人*/Value(${mail.from})private String from;/*** 收件人*/Value(${mail.to})private String to;/*** 服务器地址*/Value(${mail.server})private String server;/*** 服务器端口号*/Value(${mail.port})private String port;
}ConfigurationPropertiesprefix(前缀
例如下面的用户信息配置
#用户信息
user:address: 北京市朝阳区安外大羊坊八号院8号楼2单元112name: 张三age: 12sex: 男hobbies:- 足球- 游泳- 骑行- 篮球- 棒球
获取用户信息
package com.burns.yuanma.admin.pojo;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;Component
ConfigurationProperties(prefix user)
Data
public class UserProperties {/*** 姓名*/private String name;/*** 年齡*/private int age;/*** 性別*/private String sex;/*** 郵件*/private String email;/*** 住址*/private String address;/*** 愛好*/private String[] hobbies;
}访问接口获取信息
上面两种方式都可以获取信息
下面是controller
package com.burns.yuanma.admin.controller;import com.burns.yuanma.admin.pojo.MailProperties;
import com.burns.yuanma.admin.pojo.UserProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;RestController
public class PropertiesController {Autowiredprivate UserProperties userProperties;Autowiredprivate MailProperties mailProperties;RequestMapping(/userInfo)public String userInfo(){System.out.println(userProperties.toString());return userProperties.toString();}RequestMapping(/mailInfo)public String mailInfo(){System.out.println(mailProperties.toString());return mailProperties.toString();}}访问浏览器 用户信息
http://localhost:9999/yuanma/userInfohttp://localhost:9999/yuanma/userInfo 邮件信息
http://localhost:9999/yuanma/mailInfohttp://localhost:9999/yuanma/mailInfo