arqsartax
arqsartax

Reputation: 45

Create/Initialize a bean before some other bean in SpringBoot Application

I have my Application.java like this:

package server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;    

@SpringBootApplication(exclude=HibernateJpaAutoConfiguration.class)
public class Application {

    @Bean
    public HibernateJpaSessionFactoryBean sessionFactory(EntityManagerFactory emf) {
        HibernateJpaSessionFactoryBean fact = new HibernateJpaSessionFactoryBean();
        fact.setEntityManagerFactory(emf);
        return fact;
    }

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

My problem it's that I want to create the bean for the sessionFactory before starts to work the @ComponentScan of the @SpringBootApplication but I don't know how can I do that

Upvotes: 0

Views: 1051

Answers (1)

Narendra Jaggi
Narendra Jaggi

Reputation: 1338

From what I understood is you want to first initialize the HibernateJpaSessionFactoryBean before any bean gets initialize which actually uses the HibernateJpaSessionFactoryBean.
Try using the @DependsOn
You can move the HibernateJpaSessionFactoryBean to a different config class for eg. go through this example

Upvotes: 3

Related Questions