0

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(...);
  }
}

1 Answer 1

0

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 :)

Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome!! Could you vote the answer as useful, so other users can learn from it? :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.