javaspring-bootmongock

field annotated with `@value` is not initialized in mongock configuration


I need to assure data migration using mongock. The @ChangeUnit class holds the logic for migration. It has a field annotated with @Value which is always null, even though I properly initialized in application.properties:

mongock.migration-scan-package=my.package
login-secret=test

Then the MigrationConfiguration looks as follows:

@ChangeUnit(id = "test", order = "001", author = "test")
@RequiredArgsConstructor
@Configuration
public class InitUsersChangeLog {

  private final MyService service;
  private final MongoTemplate template;

  @Value("${login-secret}")
  private String LOGIN;

  @Execution
  public void initUser() {
    service.create(User.builder().login(LOGIN).build());
  }
}

Main class:

@EnableMongock
@SpringBootApplication
public class MailServiceApplication {...}

My assumption is that this value is not injected properly into the MongockConfiguration bean. I tried to configure the bean manually (without using mongock.migration-scan-package=my.package) in the properties, but with no success.


Solution

  • As Mongock currently doesn't support @Value annotation you can try to use getProperty method from Environment bean. Environment bean can be injected same as other beans using constructor or Lombok annotations.

    You want to change this:

    @Value("your.key.property")
    

    to that:

    private final Environment env;
      
    public void method(){
      env.getProperty("your.key.property")
    }