如何读取基于配置属性的值传递?
How Read configuration properties based value pass?
我有 spring 用户发送参数的启动应用程序,根据这个参数,应该得到配置。
我创建了文件名配置类型“Configration_Types.properies”,现在如何根据参数传递读取配置我不想创建数据库来查找它?
Type1=Type1
Type1.width=60
Type1.heght=715
Type2=Type2
Type2.width=100
Type2.heght=720
Type3=Type3
Type3.width=100
Type3.heght=700
Type4=Type4
Type4.width=450
Type4.heght=680
Type5=Type5
Type5.width=270
Type5.heght=750
例如传递 type4 应该得到配置
Type4
450
680
函数可能是这样的:
public void readPropertyByType(String type)
{
InputStream input = null;
try
{
Properties prop = new Properties();
// if your type is Type4, the typeKey will be Type4. for compare data
String typeKey = type + ".";
String filename = "config.properties";
input = new FileInputStream(filename);
prop.load(input);
String typeInformation = "";
Enumeration< ? > e = prop.propertyNames();
while (e.hasMoreElements())
{
String key = (String)e.nextElement();
if (key.indexOf(typeKey) > 0)
{
typeInformation = typeInformation + prop.getProperty(key);
}
}
System.out.println("The data of type " + type + "is :" + typeInformation);
}
catch (IOException ex)
{
ex.printStackTrace();
}
finally
{
if (input != null)
{
try
{
input.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
- 但是你为你的 属性 文件做了更好的设计。
- 如果 属性 文件的顺序永远不会改变,你可以停止
当您获得所有预期数据时运行,不需要循环整个
文件。
希望对您有所帮助。
---更新---
属性 文件可能是这样的:
- 选项 1
Type4.width=450
Type4.heght=680
Type5.width=450
Type5.heght=680
- 选项 2
Type4=450, 680
Type5=450, 680
根据每个选项,您可以在获得预期数据时中断 while 循环。
如果您可以修改您的 属性 文件 Configration_Types.properies,例如:-
type[0].width=60
type[0].heght=715
type[1].width=100
type[1].heght=720
type[2].width=100
type[2].heght=700
type[3].width=450
type[3].heght=680
type[4].width=270
type[4].heght=750
而您的 class 消耗 属性 文件中的 属性 值将是:-
@Component
@ConfigurationProperties("type") // prefix type, find type.* values
public class GlobalProperties {
private List<Type> type = new ArrayList<Type>();
//getters and setters
public static class Type
{
private int width;
private int heght;
// getter setter
}
并且根据用户参数,您可以访问 arraylist 中的值。
希望对您有所帮助:
我有 spring 用户发送参数的启动应用程序,根据这个参数,应该得到配置。
我创建了文件名配置类型“Configration_Types.properies”,现在如何根据参数传递读取配置我不想创建数据库来查找它?
Type1=Type1
Type1.width=60
Type1.heght=715
Type2=Type2
Type2.width=100
Type2.heght=720
Type3=Type3
Type3.width=100
Type3.heght=700
Type4=Type4
Type4.width=450
Type4.heght=680
Type5=Type5
Type5.width=270
Type5.heght=750
例如传递 type4 应该得到配置
Type4
450
680
函数可能是这样的:
public void readPropertyByType(String type)
{
InputStream input = null;
try
{
Properties prop = new Properties();
// if your type is Type4, the typeKey will be Type4. for compare data
String typeKey = type + ".";
String filename = "config.properties";
input = new FileInputStream(filename);
prop.load(input);
String typeInformation = "";
Enumeration< ? > e = prop.propertyNames();
while (e.hasMoreElements())
{
String key = (String)e.nextElement();
if (key.indexOf(typeKey) > 0)
{
typeInformation = typeInformation + prop.getProperty(key);
}
}
System.out.println("The data of type " + type + "is :" + typeInformation);
}
catch (IOException ex)
{
ex.printStackTrace();
}
finally
{
if (input != null)
{
try
{
input.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
- 但是你为你的 属性 文件做了更好的设计。
- 如果 属性 文件的顺序永远不会改变,你可以停止 当您获得所有预期数据时运行,不需要循环整个 文件。
希望对您有所帮助。
---更新---
属性 文件可能是这样的:
- 选项 1
Type4.width=450
Type4.heght=680
Type5.width=450
Type5.heght=680
- 选项 2
Type4=450, 680
Type5=450, 680
根据每个选项,您可以在获得预期数据时中断 while 循环。
如果您可以修改您的 属性 文件 Configration_Types.properies,例如:-
type[0].width=60
type[0].heght=715
type[1].width=100
type[1].heght=720
type[2].width=100
type[2].heght=700
type[3].width=450
type[3].heght=680
type[4].width=270
type[4].heght=750
而您的 class 消耗 属性 文件中的 属性 值将是:-
@Component
@ConfigurationProperties("type") // prefix type, find type.* values
public class GlobalProperties {
private List<Type> type = new ArrayList<Type>();
//getters and setters
public static class Type
{
private int width;
private int heght;
// getter setter
}
并且根据用户参数,您可以访问 arraylist 中的值。
希望对您有所帮助: