测试
package com.sky.springboot;
import com.sky.springboot.pojo.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(SpringbootApplication.class, args);
User user = (User) run.getBean("user");
System.out.println("user:"+user);
}
}
pojo
package com.sky.springboot.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "user")
@PropertySource(value = "classpath:config.properties")
public class User {
private int userid;
private String uname;
private String upwd;
private String sex;
@Override
public String toString() {
return "User{" +
"userid=" + userid +
", uname='" + uname + '\'' +
", upwd='" + upwd + '\'' +
", sex='" + sex + '\'' +
'}';
}
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpwd() {
return upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public User() {
}
public User(int userid, String uname, String upwd, String sex) {
this.userid = userid;
this.uname = uname;
this.upwd = upwd;
this.sex = sex;
}
}
配置
user.userid=100
user.uname=kkk