properties综合案例

目标:
通过反射技术动态创建配置文件中指定类的对象,对象的各个属性值也由配置文件的到

将javaBean的数据以配置文件的方式写进properties中,读取配置文件写进BeanConfig进行封装,在测试类中去读BeanConfig中封装的数据,并以反射的方式根据读取的数据创建对象,给对象的属性设置值。
反射的好处:我们不用将数据写死到java代码中,可以使用配置文件将数据写入配置文件中。提高了代码的灵活性。

结构图:
在这里插入图片描述
实现步骤:

  1. 创建数据类
  2. 创建配置文件,bean.properties和data.properties
  3. 创建BeanConfig对象
  4. 模式数据测试BeanConfig对象(直接手动输入配置文件中的值,显示测试)
  5. 将模拟输入,改为加载配置文件
  6. 在模拟输入创建好BeanConfig对象前提下,动态实例化对象
  7. 整合,即将最后的测试中的模拟输入,改为加载配置文件形式

项目目录结构:
在这里插入图片描述

具体实现:

创建数据类
User:

public class User {

	private String uid;
	private String userName;
	private String password;

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	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;
	}

	@Override
	public String toString() {
		return "User [uid=" + uid + ", userName=" + userName + ", password=" + password + "]";
	}
}

Book:

public class Book {

	private String bid;
	private String title;
	private String price;

	public String getBid() {
		return bid;
	}

	public void setBid(String bid) {
		this.bid = bid;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getPrice() {
		return price;
	}

	public void setPrice(String price) {
		this.price = price;
	}

	@Override
	public String toString() {
		return "Book [bid=" + bid + ", title=" + title + ", price=" + price + "]";
	}
	
}

创建配置文件,bean.properties和data.properties

在这里插入图片描述
在这里插入图片描述
创建BeanConfig对象

import java.util.Properties;

public class BeanConfig {

	private String id;
	private String className;
	private Properties props = new Properties();

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public Properties getProps() {
		return props;
	}

	public void setProps(Properties props) {
		this.props = props;
	}

	@Override
	public String toString() {
		return "BeanConfig [id=" + id + ", className=" + className + ", props=" + props + "]";
	}
	
}

模式数据测试BeanConfig对象(直接手动输入配置文件中的值,显示测试)

import java.util.Properties;

import org.junit.Test;

import com.gang.domain.BeanConfig;

public class TestDemo {
	
	@Test
	public void demo(){
		
		//模拟数据,直接输入,测试BeanCongig对象
		
		//模拟加载bean.properties文件
		BeanConfig bc=new BeanConfig();
		bc.setId("i1001");
		bc.setClassName("com.gang.domain.User");
		
		//模拟加载data.properties文件
		Properties props=new Properties();
		props.setProperty("uid", "u1001");
		props.setProperty("usrName", "小明");
		props.setProperty("password", "123456");
		
		bc.setProps(props);
		
		//显示输出
		System.out.println(bc);
	}
	
	
}

将模拟输入,改为加载配置文件

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Properties;

import org.junit.Test;

import com.gang.domain.BeanConfig;

public class TestDemo2 {

	@Test
	public void demo() throws IOException {

		// 实际加载配置文件

		BeanConfig bc = new BeanConfig();

		// 创建properties对象,用于加载对应文件
		Properties beanprops = new Properties();

		// 创建输入流
		Reader readbean = new InputStreamReader(new FileInputStream("bean.properties"), "UTF-8");

		// 加载文件
		beanprops.load(readbean);

		// 设置对应的值
		bc.setId(beanprops.getProperty("id"));
		bc.setClassName(beanprops.getProperty("className"));

		// 创建properties对象,用于加载对应文件
		Properties dataprops = new Properties();
		
		//加载data.properties文件
		
		// 创建输入流
		Reader readdata = new InputStreamReader(new FileInputStream("data.properties"), "UTF-8");
		
		// 加载文件
		dataprops.load(readdata);
		
		//设置BeanConfig对象中的properties对象
		bc.setProps(dataprops);
		
		//测试输出
		System.out.println(bc);
	}

}

在模拟输入创建好BeanConfig对象前提下,动态实例化对象

import java.lang.reflect.Method;
import java.util.Properties;

import org.junit.Test;

import com.gang.domain.BeanConfig;

public class TestDemo3 {
	
	@Test
	public void demo() throws  Exception{
		
		//模拟数据,直接输入,测试BeanCongig对象
		
		//模拟加载bean.properties文件
		BeanConfig bc=new BeanConfig();
		bc.setId("i1001");
		bc.setClassName("com.gang.domain.User");
		
		//模拟加载data.properties文件
		Properties props=new Properties();
		props.setProperty("uid", "u1001");
		props.setProperty("userName", "小明");
		props.setProperty("password", "123456");
		
		bc.setProps(props);
		
		//显示输出
		System.out.println(bc);
		
		//通过BeanConfig对象中的数据,用反射技术创建对象
		
		//创建对应类的实例对象:
		//1.得到Class
		Class clazz=Class.forName(bc.getClassName());
		//2.实例对象
		Object obj=clazz.newInstance();
		
		//调用对应的set方法为成员变量赋值
		//遍历data.properties文件,依次设置
		for(String name:bc.getProps().stringPropertyNames()){
			
			//得到方法名 格式为:get+name(其中name第一个字母要大写  例如setUid(String uid))
			String methodName="set"+name.substring(0,1).toUpperCase()+name.substring(1);
			//System.out.println(methodName);
			Method method=clazz.getMethod(methodName, String.class);
			
			//调用方法
			//System.out.println(bc.getProps().getProperty(name));
			method.invoke(obj, bc.getProps().getProperty(name));
		}
		
		//显示对象数据
		System.out.println(obj);
		
	}
	
	
}

整合,即将最后的测试中的模拟输入,改为加载配置文件形式

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Method;
import java.util.Properties;

import org.junit.Test;

import com.gang.domain.BeanConfig;

public class TestDemo4 {
	
	@Test
	public void demo() throws  Exception{
		
		//整合
		
		//调用方法,初始化BeanConfig对象
		BeanConfig bc=getBean();
		
		
		//通过BeanConfig对象中的数据,用反射技术创建对象
		
		//创建对应类的实例对象:
		//1.得到Class
		Class clazz=Class.forName(bc.getClassName());
		//2.实例对象
		Object obj=clazz.newInstance();
		
		//调用对应的set方法为成员变量赋值
		//遍历data.properties文件,依次设置
		for(String name:bc.getProps().stringPropertyNames()){
			
			//得到方法名 格式为:get+name(其中name第一个字母要大写  例如setUid(String uid))
			String methodName="set"+name.substring(0,1).toUpperCase()+name.substring(1);
			//System.out.println(methodName);
			Method method=clazz.getMethod(methodName, String.class);
			
			//调用方法
			//System.out.println(bc.getProps().getProperty(name));
			method.invoke(obj, bc.getProps().getProperty(name));
		}
		
		//显示对象数据
		System.out.println(obj);
		
	}
	
	//加载配置文件得到BeanCongig对象
	public BeanConfig getBean() throws IOException {

		// 实际加载配置文件

		BeanConfig bc = new BeanConfig();

		// 创建properties对象,用于加载对应文件
		Properties beanprops = new Properties();

		// 创建输入流
		Reader readbean = new InputStreamReader(new FileInputStream("bean.properties"), "UTF-8");

		// 加载文件
		beanprops.load(readbean);

		// 设置对应的值
		bc.setId(beanprops.getProperty("id"));
		bc.setClassName(beanprops.getProperty("className"));

		// 创建properties对象,用于加载对应文件
		Properties dataprops = new Properties();
		
		//加载data.properties文件
		
		// 创建输入流
		Reader readdata = new InputStreamReader(new FileInputStream("data.properties"), "UTF-8");
		
		// 加载文件
		dataprops.load(readdata);
		
		//设置BeanConfig对象中的properties对象
		bc.setProps(dataprops);
		
		//测试输出
		return bc;
	}
	
	
}

运行结果:
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: SystemProperties是一个Android平台上的类,可以用于读取和写入系统属性。在Android开发,可以通过这个类获取一些系统的信息,例如设备的制造商、型号、固件版本等。 例如,在Java代码,可以使用以下方式获取设备的制造商信息: String manufacturer = SystemProperties.get("ro.product.manufacturer"); 这样就可以获取到设备的制造商信息,这个值会在系统属性存储。 另一个用法是设置系统属性。例如,可以通过以下方式将一个属性设置为一个值: SystemProperties.set("my.custom.property", "my custom value"); 这个属性的值现在被设置为“my custom value”。 总之,SystemProperties类可以用于读取和设置一些系统属性,这在一些Android应用程序非常有用。 ### 回答2: SystemProperties是Android系统的一个类,用于访问和管理系统属性。系统属性是存储在Android设备上的一些全局变量,可以通过SystemProperties类来获取和修改这些属性。 以下是一个SystemProperties的用法案例: 首先,我们可以使用SystemProperties.getProperty()方法来获取一个系统属性的值。例如,我们想要获取设备的厂商信息,可以使用如下代码: String manufacturer = SystemProperties.getProperty("ro.product.manufacturer"); 接下来,我们可以使用SystemProperties.get()方法来获取一个系统属性的值,并且可以指定一个默认值。例如,我们想要获取设备的序列号,若设备没有序列号属性,则返回一个默认值"unknown",可以使用如下代码: String serialNumber = SystemProperties.get("ro.serialno", "unknown"); 另外,我们还可以使用SystemProperties.set()方法来修改一个系统属性的值。需要注意的是,这个方法需要申请修改系统属性的权限。例如,我们想要修改设备的网络类型属性为LTE,可以使用如下代码: SystemProperties.set("ro.telephony.network", "LTE"); 此外,还可以使用SystemProperties.getBoolean()方法来获取一个系统属性的布尔值。例如,我们想要判断设备是否支持蓝牙功能,可以使用如下代码: boolean isBluetoothSupported = SystemProperties.getBoolean("ro.bluetooth.supported", false); 总结起来,SystemProperties类提供了方便的接口来访问和管理Android设备的系统属性,可以用于获取设备的相关信息、修改系统属性的值以及判断系统属性的布尔值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值