【问题标题】:How Read configuration properties based value pass?如何读取基于配置属性的值传递?
【发布时间】:2018-09-12 12:40:29
【问题描述】:

我有用户发送参数的spring boot应用程序,根据这个参数,应该有配置。

我创建了文件名配置类型“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

【问题讨论】:

  • 你有没有尝试过?请分享您的代码。我确定在这里可以找到描述:spring.io
  • 您是如何尝试访问属性文件的?使用 XML 还是通过 java 类?
  • 我几乎不明白你真正想要什么,但你检查过:access-properties-programmaticallyread-values-from-properties 如果我理解正确,我会认为它是重复的......
  • @Adya java 类

标签: java spring spring-boot spring-boot-configuration


【解决方案1】:

函数可能是这样的:

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 循环。

【讨论】:

  • 这是最好的方法吗?您能否向我解释一下您的属性文件的更好设计
【解决方案2】:

如果您可以修改您的属性文件 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

而您用于使用属性文件中的属性值的类将是:-

@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 访问值。

希望得到帮助:

【讨论】:

  • 还有另一个选项,而不是在 **properties 文件中进行索引 ** 我更喜欢按名称命名,这样会更容易混淆
  • 是的,这也是可能的,但我认为您根据用户输入(1,2 等)尝试的方法没有太大差异。您可以使用 LinkedList 来维护顺序并通过索引获取属性。
猜你喜欢
  • 1970-01-01
  • 2019-05-22
  • 2018-02-06
  • 1970-01-01
  • 2020-07-17
  • 1970-01-01
  • 1970-01-01
  • 2018-08-08
  • 1970-01-01
相关资源
最近更新 更多