【问题标题】:How to set up init method with file constants for Spring Application?如何使用 Spring Application 的文件常量设置 init 方法?
【发布时间】:2022-02-02 09:59:45
【问题描述】:

我对 Java Spring 的使用还很陌生,所以这是我的问题。 在我的应用程序启动之前,我需要将一些信息注入数据库。我的意思是,我知道如何使用@Bean init-method,但我不想在 .properties 文件中硬编码常量。 这是我的临时解决方案(数据已更改):

@Bean
ApplicationRunner init(RoleRepo roles, UserRepo users, SettingsRepo settings, RoomRepo rooms, GroupRepo groups) {

    String[][] data_roles = {
            {"1", "ROLE_UNCOMP"},
            {"2", "ROLE_USER"},
            {"3", "ROLE_OPERATOR"},
            {"4", "ROLE_ADMIN"}
    };

    String pass = bCryptPasswordEncoder().encode("qwe");

    String[][] data_users = {
            {"1", "User0", "qwe", pass, "123"},
            {"2", "User1", "qwe1", pass, "124"},
            {"3", "User2", "qwe2", pass, "125"},
    };

    String[][] data_settings = {
            {"1", "booking_days", "3"},
            {"2", "auto_registration", "true"},
            {"3", "auto_booking", "false"},
            {"4", "urgent_booking_time", "15"}
    };

    String[][] data_rooms = {
            {"1", "Лекционный зал", "https://href1", "all_day"},
            {"2", "Малая переговорная", "https://href2", "all_day"},
            {"3", "Переговорная", "https://href3", "all_day"},
            {"4", "Скайповая 1", "https://href4", "all_day"},
            {"5", "Скайповая 2", "https://href5", "all_day"},
            {"6", "Скайповая 3", "https://href6", "all_day"},
            {"7", "Скайповая 4", "https://href7", "all_day"},
            {"8", "Скайповая 5", "https://href8", "all_day"}
    };

    String[][] data_groups = {
            {"1", "Group1"},
            {"2", "Group2"},
            {"3", "Group3"},
            {"4", "Group4"},
            {"5", "Group5"},
            {"6", "Group6"},
            {"7", "Group7"},
            {"8", "Group8"},
            {"9", "Group9"},
            {"10", "Group10"},
            {"11", "Group11"}
    };

    return args -> {
        Stream.of(data_roles).forEach(a -> {
            Role role = new Role(Long.parseLong(a[0]), a[1]);
            roles.save(role);
        });
        Stream.of(data_groups).forEach(a -> {
            Group group = new Group(Long.parseLong(a[0]), a[1]);
            groups.save(group);
        });
        Stream.of(data_users).forEach(a -> {
            User user = new User(Long.parseLong(a[0]), a[1], a[2], a[3], Long.parseLong(a[4]));
            user.setRoles(Collections.singleton(new Role(3L, "ROLE_ADMIN")));
            user.setGroups(Collections.singleton(new Group(1L, "Group1")));
            users.save(user);
        });
        Stream.of(data_settings).forEach(a -> {
            Settings setting = new Settings(Long.parseLong(a[0]), a[1], a[2]);
            settings.save(setting);
        });
        Stream.of(data_rooms).forEach(a -> {
            Room room = new Room(Long.parseLong(a[0]), a[1], a[2], a[3]);
            rooms.save(room);
        });
    };
}

但是这个IS硬编码,更重要的是,每个客户端都有自己的常量列表。 您能帮我解决这样的解决方案吗,只需将文件名作为 .jar 启动器参数/路径变量传递并从该文件生成默认值就足够了。 例如:

java jar application.jar -constants ~./User/constants.xml

【问题讨论】:

    标签: java spring spring-data-jpa


    【解决方案1】:

    我认为您可以将 ApplicationArguments 自动连接到您的 bean 初始化方法,然后从 jar 参数中获取文件名。

    【讨论】:

    • 我不太确定这是否适用于 docker 和容器化,但您的想法给了我一些提示,非常感谢!
    【解决方案2】:

    答案很明显——注解@Value("${your_arg}") 会自动检查您的application.properties 文件,如果没有此类属性,则尝试在命令行参数中找到它(所有这些操作都在上下文启动之前完成)。这就是为什么你可以简单地做这样的事情:

    @Value("${config}")
    private String config_file;
    
    @Bean
    ApplicationRunner init(DaoRepo args...) {
        //Here comes DB seeding from config_file;
    }
    

    之后,您可以简单地构建您的 jar 文件并启动它:

    java jar ../target/yor_app.jar --config=path/to/your/config_file.extension
    

    它适用于 JpaRepositories 和 DB 播种,但我没有检查过 application.properties 覆盖。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-26
      • 1970-01-01
      • 2020-04-19
      相关资源
      最近更新 更多