做網(wǎng)站大概需要多少錢(qián)深圳最新疫情
目錄
- 1. 使用@Value和@ConfigurationProperties
- 2. 使用@PropertySource
- 創(chuàng)建Person.java
- 寫(xiě)一個(gè)測(cè)試類(lèi)
- 3. 使用@ImportResource
- Student類(lèi)
- 創(chuàng)建beans.xml
- 在主類(lèi)中引入
- 測(cè)試
- 其他
- 心得
1. 使用@Value和@ConfigurationProperties
這里不加贅述了,前面我也發(fā)過(guò),這里就放個(gè)鏈接吧
@Value獲取值和@ConfigurationProperties獲取值用法及比較(springboot)
2. 使用@PropertySource
創(chuàng)建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")
// 這個(gè)是前綴的意思
@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 Map<String, Integer> maps;private List<String> 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 Map<String, Integer> getMaps() {return maps;}public void setMaps(Map<String, Integer> maps) {this.maps = maps;}public List<String> getLists() {return lists;}public void setLists(List<String> 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 +'}';}
}
創(chuàng)建person.properties
person2.name="李四"
person2.sex=男
person2.birthday=2022-02-07
person2.age=18
person2.maps.keys1=16
person2.maps.keys2=16
person2.lists=[12,24,57]
person2.address="保定廉恥"
person2.dog.name=${random.value}
寫(xiě)一個(gè)測(cè)試類(lèi)
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);}
}
輸出結(jié)果:
Person{name='"岳軒子"', sex=M,
birthday=Sun Dec 26 00:00:00 CST 2021, age=18,
address='"保定武漢"', maps={keys2=16, keys1=16}, lists=[[12, 24, 57]],
dog=Dog{name='cdab390f55c9f8a6bbb420cd15607add'}}
注:如果顯示亂碼,設(shè)置文件編碼為utf-8
3. 使用@ImportResource
Student類(lèi)
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 +'}';}
}
創(chuàng)建beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="student" class="com.example.springbootdaily.model.Student"><property name="name" value="李四"/><property name="age" value="18"/></bean>
</beans>
在主類(lèi)中引入
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);}}
測(cè)試
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);}
}
運(yùn)行結(jié)果:
Student{name='李四', age=18}
其他
我們可以導(dǎo)入配置文件處理器,以后編寫(xiě)配置就有提示了
<!‐‐導(dǎo)入配置文件處理器,配置文件進(jìn)行綁定就會(huì)有提示‐‐>
依賴(lài):
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring‐boot‐configuration‐processor</artifactId><optional>true</optional>
</dependency>
心得
平常還是要多多總結(jié)的。
謝謝大家支持!!!!