如何使用Java读取到properties文件中的内容,并且把它封装到JavaBean中,以供随时使用
user1.properties
user.id=1
user.username=lucy
user.password=111222
public static void getProperties(){
Properties pps = new Properties();
try {
pps.load(new FileInputStream("D:\\java-oxygen\\IDEA CODE\\springboottest1\\src\\main\\resources\\user1.properties"));
} catch (IOException e) {
e.printStackTrace();
}
Enumeration enum1 = pps.propertyNames();//得到配置文件的名字
while(enum1.hasMoreElements()) {
String strKey = (String) enum1.nextElement();
String strValue = pps.getProperty(strKey);
System.out.println(strKey + "=" + strValue);
//封装到JavaBean。
//......
}
}
User
package com.limi.entity;
import org.springframework.stereotype.Component;
//只有在容器中的组件,才会拥有SpringBoot提供的强大功能
@Component
public class User {
private Integer id;
private String userName;
private String passWord;
public User(){}
public User(Integer id, String userName, String passWord) {
this.id = id;
this.userName = userName;
this.passWord = passWord;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
", passWord='" + passWord + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
}
如果属性值不在application.properties中, 则需使用@PropertySources导入properties文件
MyConfig
package com.limi.config;
import com.limi.entity.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
// 默认取application.properties, 需要读取多个properties可以用@PropertySources设置
@PropertySource(value = {"user1.properties"})
public class MyConfig {
@Bean
public User user1(){
return new User();
}
}
MainApplication
package com.limi;
import com.limi.entity.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
//1、返回我们IOC容器
ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
User user1 = (User)run.getBean("user1");
System.out.println(user1);
}
}
User
package com.limi.entity;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "user")
public class User {
private Integer id;
private String userName;
private String passWord;
public User(){}
public User(Integer id, String userName, String passWord) {
this.id = id;
this.userName = userName;
this.passWord = passWord;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
", passWord='" + passWord + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
}
MyConfig
package com.limi.config;
import com.limi.entity.User;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@EnableConfigurationProperties(User.class)
// 默认取application.properties, 需要读取多个properties可以用@PropertySources设置
@PropertySource(value = {"user1.properties"})
public class MyConfig {
@Bean
public User user1(){
return new User();
}
}
MainApplication
package com.limi;
import com.limi.entity.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
//1、返回我们IOC容器
ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
User user1 = (User)run.getBean("user1");
System.out.println(user1);
}
}
这个注解的功能就是将某个类创建为组件导入到IOC容器中
User
package com.limi.entity;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "user")
public class User {
private Integer id;
private String userName;
private String passWord;
public User(){}
public User(Integer id, String userName, String passWord) {
this.id = id;
this.userName = userName;
this.passWord = passWord;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
", passWord='" + passWord + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
}
MyConfig
package com.limi.config;
import com.limi.entity.User;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
@Configuration
@EnableConfigurationProperties(User.class)
// 默认取application.properties, 需要读取多个properties可以用@PropertySources设置
@PropertySource(value = {"user1.properties"})
@Import(User.class)
public class MyConfig {
}
MainApplication
package com.limi;
import com.limi.entity.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
//1、返回我们IOC容器
ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
User user1 = (User)run.getBean("com.limi.entity.User");
System.out.println(user1);
}
}
# 设置端口号,如果不设置,默认是8080
server.port=8081
user1.properties
user.id=1
user.username=lucy
user.password=111222
User
package com.limi.entity;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "user")
public class User {
private Integer id;
private String userName;
private String passWord;
public User(){}
public User(Integer id, String userName, String passWord) {
this.id = id;
this.userName = userName;
this.passWord = passWord;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
", passWord='" + passWord + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
}
MyConfig
package com.limi.config;
import com.limi.entity.User;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
@Configuration
@EnableConfigurationProperties(User.class)
// 默认取application.properties, 需要读取多个properties可以用@PropertySources设置
@PropertySource(value = {"user1.properties"})
@Import(User.class)
public class MyConfig {
}
MainApplication
package com.limi;
import com.limi.entity.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
//1、返回我们IOC容器
ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
User user1 = (User)run.getBean("com.limi.entity.User");
System.out.println(user1);
}
}