Skip to content
Advertisement

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

I’m pretty new in Java Spring usage, so here’s my question. I need to inject some information into DB before my application starts. I mean, I know how to use @Bean init-method, but i dont’t want to hardcode constants even in .properties file. Here’s my temporary solution (data is changed):

@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);
        });
    };
}

But this IS hardcode, what’s more, every client will have its own list of constants. Can You help me with such a solution, where it’s enough to pass a file name as a .jar starter parameter/Path variable and generate defaults from that file. For example:

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

Advertisement

Answer

The answer is rather obvious – annotation @Value("${your_arg}") automatically checks your application.properties file and if there’s no such property tries to find it in command line arguments (all this actions are done before context starts). That’s why you can simply make something like this:

@Value("${config}")
private String config_file;

@Bean
ApplicationRunner init(DaoRepo args...) {
    //Here comes DB seeding from config_file;
}

After that, you can simply build your jar file and start it with:

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

It works fine with JpaRepositories and DB seeding, but I haven’t checked it with application.properties overriding.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement