如何为 Spring 应用程序设置带有文件常量的 init 方法?

How to set up init method with file constants for Spring Application?

我在 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);
        });
    };
}

但是这个硬编码,更重要的是,每个客户端都会有自己的常量列表。 你能帮我解决这样的问题吗?它足以将文件名作为 .jar 启动器 parameter/Path 变量传递并从该文件生成默认值。 例如:

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

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

答案很明显 - 注释 @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 覆盖检查它。