怎样建立网站的快捷方式,网站建设公司销售技巧,沈阳建设信息网,wordpress无法进入登录页这里写目录标题1、基本语法2、给java bean注入值3、测试1、基本语法
# yaml 配置文件写法#xff0c;代替properties写法
# 严格区分空格# 内注入到配置类中
server:port: 8081# 对象
student:name: jackage: 3# 行内写法 map
student1: {name: jack, age: 3}# array or coll…
这里写目录标题1、基本语法2、给java bean注入值3、测试1、基本语法
# yaml 配置文件写法代替properties写法
# 严格区分空格# 内注入到配置类中
server:port: 8081# 对象
student:name: jackage: 3# 行内写法 map
student1: {name: jack, age: 3}# array or collections
pets:- dog- cat- turtlepets1: [dog, cat, turtle]# 还可以写随机数 或者 uuid
uuid: ${random.uuid}
random: ${random.int}2、给java bean注入值
核心
ConfigurationProperties(prefix person)
// yaml 里面写person的属性
// 应用数据源配置..application.yaml
# yaml 对象注入
dog:name: 八公age: 6person:person-name: abc163.com # bitqian_${random.uuid}happy: falsemaps: {k1: v1, k2: v2}age: ${random.int}hobbyList:- girl- code- bookdog:name: 拉布拉多age: 3server:port:8081# https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config这里以dog和person实体类对各种数据类型说明dog 这里用了properties加载配置官方推荐ymal
package cn.bitqian.entity;import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;/*** author echo lovely* date 2020/9/26 21:45*/
Component
// ConfigurationProperties(prefix dog)
PropertySource(value classpath:application.properties)
public class Dog {// value SPEL Value(${server.port})private String name;private Integer age;public Dog() {}public Dog(String name, Integer age) {this.name name;this.age age;}public String getName() {return name;}public void setName(String name) {this.name name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age age;}Overridepublic String toString() {return Dog{ name name \ , age age };}
}
person类
package cn.bitqian.entity;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;import javax.validation.constraints.Email;import java.util.List;
import java.util.Map;/*** author echo lovely* date 2020/9/26 21:46*/
Component
// 对应前缀
ConfigurationProperties(prefix person) // setter/getter 与yaml对应
Validated // 数据校验
public class Person {Email(message 必须是邮箱格式)private String personName;private Boolean happy;private Integer age;private MapString, Object maps;private ListObject hobbyList;private Dog dog;public Person() {}public Person(String name, Boolean happy, Integer age,MapString, Object maps, ListObject hobbyList, Dog dog) {this.personName name;this.happy happy;this.age age;this.maps maps;this.hobbyList hobbyList;this.dog dog;}public String getPersonName() {return personName;}public void setPersonName(String personName) {this.personName personName;}public Boolean getHappy() {return happy;}public void setHappy(Boolean happy) {this.happy happy;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age age;}public MapString, Object getMaps() {return maps;}public void setMaps(MapString, Object maps) {this.maps maps;}public ListObject getHobbyList() {return hobbyList;}public void setHobbyList(ListObject hobbyList) {this.hobbyList hobbyList;}public Dog getDog() {return dog;}public void setDog(Dog dog) {this.dog dog;}Overridepublic String toString() {return Person{ personName personName \ , happy happy , age age , maps maps , hobbyList hobbyList , dog dog };}
}
3、测试
package cn.bitqian;import cn.bitqian.entity.Dog;
import cn.bitqian.entity.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;import javax.annotation.Resource;SpringBootTest
class Springboot02ApplicationTests {Resourceprivate Dog dog;AutowiredQualifier(value person)private Person person;Testvoid contextLoads() {System.out.println(dog);System.out.println(person);}}
age是随机的intpersonName 做了邮箱验证