yml文件:
yml属性注入:
student:
stu_name: 沈刚伟
stu_age: 123
stu_email: 123456@qq.com
user_id: ${random.int[1000,2000]} //随机数
user_name: ${student.name2:吴没}//调用properties里面配置 如果为空默认为后面的
birthday: 2018/2/1
local: #{name: 沈刚伟,aa: 杭州}
name: 沈刚伟
aa: 杭州
habbies:
- 足球
- 篮球
skills: [编程,金融]
#- 编程
#- 金融
pet: {pet_name: 张三1,pet_sex: 4}
#pet_name: 张三
#pet_sex: 4
Java
package com.xiaonuo.pojo;
import org.springframework.beans.factory.annotation.Value;
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.*;
@Component
@ConfigurationProperties(prefix = "student")
@Validated
public class Student {
@Value("${student.stu_name}")
private String stu_name;
@Value("${student.stu_age}")
private Integer stu_age;
@Email
private String stu_email;
@Value("[谁给我,aaa]")
private List<String> hobbies;
private Map<String,Object> map;
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
public String getStu_email() {
return stu_email;
}
public List<String> getHobbies() {
return hobbies;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public void setStu_email(String stu_email) {
this.stu_email = stu_email;
}
public String getStu_name() {
return stu_name;
}
public void setStu_name(String stu_name) {
this.stu_name = stu_name;
}
public Integer getStu_age() {
return stu_age;
}
public void setStu_age(Integer stu_age) {
this.stu_age = stu_age;
}
@Override
public String toString() {
return "Student{" +
"stu_name='" + stu_name + '\'' +
", stu_age=" + stu_age +
", stu_email='" + stu_email + '\'' +
", hobbies=" + hobbies +
'}';
}
}
2、创建Bean
1.可以用@ImportResource("")的方式来创建xml搭配,不过不推荐
2.推荐用Java配置
Java配置类
@Configuration public class Appconfig { @Bean public ServiceImpl service(){ ServiceImpl service = new ServiceImpl(); service.setSout("xxxxq"); return service; } }