Lucas
Lucas

Reputation: 1271

Execute MongoDB script on Spring Boot start

I need to initialize a Mongo DB with some script file like Spring do with JPA and import.sql file.. but how?

Can someone help me?

Upvotes: 3

Views: 5824

Answers (2)

iamiddy
iamiddy

Reputation: 3073

You could tag along and leverage spring-boot's initialization lifecycle, where after wiring the beans, it executes all CommandLineRunner beans.

@SpringBootApplication
public class YourApplication {
    final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private MongoRepository repo;

    @Bean
    CommandLineRunner preLoadMongo() throws Exception {
        return args -> {
            //repo.doSOmethingInMongoDB
       }

    public static void main(String[] args) {
        SpringApplication.run(YourApplication.class, args);
    }

Upvotes: 0

anand1st
anand1st

Reputation: 1176

You could use something similar that's done by mongeez. This is basically a starter for spring-boot that runs scripts before spring-data-mongodb beans are initialized.

Upvotes: 2

Related Questions