Home > Enterprise >  mongock @BeforeExecution is not executed
mongock @BeforeExecution is not executed

Time:07-14

I need to initialize admin from env variables, but @BeforeExecution method is not launched, execution directly starts in initAdmin method. Any suggestions?

@ChangeUnit(id = "init", order = "001", author = "me")
@RequiredArgsConstructor
@Component
public class InitChangeLog {

  private final MyService service;
  private final MongoTemplate template;
  private final ConfigurableEnvironment env;

  private String admin;

  @BeforeExecution
  private void setAdmin() {
    this.admin = env.getProperty("admin");
  }

  @Execution
  public void initAdmin() {
    service.create(...);
  }
}

CodePudding user response:

I can see multiple errors there that can be the reason of your issue:

  1. @Component shouldn't be used for a changeUnit. The way Mongock retrieves the changeUnits is by specifying the packages or directly the class. You can see it in the documentation

  2. Mongock offers two ways to inject your beans, in the constructor and directly in the method. Please see this section in the documentation for more information

  3. @BeforeExecution shouldn't be private.

Try correcting those points and I am confident it will work just fine :)

  • Related