0

我有 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应该得到配置

类型4

450

680

4

2 回答 2

0

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

希望这有帮助:

于 2018-04-03T10:21:51.653 回答
0

功能可能是这样的:

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.height=680

Type5.width=450

类型5.height=680

  • 选项 2

类型4=450, 680

类型5=450, 680

根据每个选项,您可以在获得预期数据时中断 while 循环。

于 2018-04-03T09:27:44.260 回答