贵阳做网站优化,做网站后端语言用什么,建筑网站叫什么盘,桐城市网站建设目录 1. 使用Value和ConfigurationProperties2. 使用PropertySource创建Person.java写一个测试类 3. 使用ImportResourceStudent类创建beans.xml在主类中引入测试 其他心得 1. 使用Value和ConfigurationProperties
这里不加赘述了#xff0c;前面我也发过#xff0c;这里就放… 目录 1. 使用Value和ConfigurationProperties2. 使用PropertySource创建Person.java写一个测试类 3. 使用ImportResourceStudent类创建beans.xml在主类中引入测试 其他心得 1. 使用Value和ConfigurationProperties
这里不加赘述了前面我也发过这里就放个链接吧 Value获取值和ConfigurationProperties获取值用法及比较springboot
2. 使用PropertySource
创建Person.java
package com.example.springbootdaily2.model;import org.springframework.format.annotation.DateTimeFormat;import java.util.Date;
import java.util.List;
import java.util.Map;Component
PropertySource(value classpath:person.properties)
// 这个是前缀的意思
ConfigurationProperties(prefix person2)
public class PersonX {private String name;private Character sex;DateTimeFormat(pattern YYYY-MM-SS)private Date birthday;private Integer age;private String address;private MapString, Integer maps;private ListString lists;private Dog dog;public String getName() {return name;}public void setName(String name) {this.name name;}public Character getSex() {return sex;}public void setSex(Character sex) {this.sex sex;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday birthday;}public String getAddress() {return address;}public void setAddress(String address) {this.address address;}public Dog getDog() {return dog;}public void setDog(Dog dog) {this.dog dog;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age age;}public MapString, Integer getMaps() {return maps;}public void setMaps(MapString, Integer maps) {this.maps maps;}public ListString getLists() {return lists;}public void setLists(ListString lists) {this.lists lists;}Overridepublic String toString() {return Person{ name name \ , sex sex , birthday birthday , age age , address address \ , maps maps , lists lists , dog dog };}
}
创建person.properties
person2.name李四
person2.sex男
person2.birthday2022-02-07
person2.age18
person2.maps.keys116
person2.maps.keys216
person2.lists[12,24,57]
person2.address保定廉耻
person2.dog.name${random.value}写一个测试类
package com.example.springbootdaily;
import com.example.springbootdaily.model.Dog;
import com.example.springbootdaily.model.Person;
import com.example.springbootdaily.model.Person2;
import com.example.springbootdaily.model.PersonX;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;RunWith(SpringRunner.class)
SpringBootTest
public class SpringTest {AutowiredPersonX personX;Testpublic void print4(){System.out.println(personX);}
}
输出结果
Person{name岳轩子, sexM,
birthdaySun Dec 26 00:00:00 CST 2021, age18,
address保定武汉, maps{keys216, keys116}, lists[[12, 24, 57]],
dogDog{namecdab390f55c9f8a6bbb420cd15607add}}注如果显示乱码设置文件编码为utf-8
3. 使用ImportResource
Student类
package com.example.springbootdaily.model;public class Student {private String name;private Integer 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 Student{ name name \ , age age };}
}
创建beans.xml
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean idstudent classcom.example.springbootdaily.model.Studentproperty namename value李四/property nameage value18//bean
/beans在主类中引入
package com.example.springbootdaily;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;SpringBootApplication
ImportResource(locations classpath:beans.xml)
public class SpringbootDailyApplication {public static void main(String[] args) {SpringApplication.run(SpringbootDailyApplication.class, args);}}
测试
package com.example.springbootdaily;import com.example.springbootdaily.model.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;RunWith(SpringRunner.class)
SpringBootTest
public class SpringTest {AutowiredStudent student;Testpublic void print5(){System.out.println(student);}
}
运行结果
Student{name李四, age18}其他
我们可以导入配置文件处理器以后编写配置就有提示了 !‐‐导入配置文件处理器配置文件进行绑定就会有提示‐‐ 依赖
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring‐boot‐configuration‐processor/artifactIdoptionaltrue/optional
/dependency心得
平常还是要多多总结的。 谢谢大家支持