SpringBoot application crash

前端 未结 1 1059
谎友^
谎友^ 2021-01-07 16:00

Tell me please where I have problem? I try to start my first application developed on Spring Boot. I already have simple web project on SpringMVC and now I like to build it

相关标签:
1条回答
  • 2021-01-07 16:30

    The root of the problem lies in the DataSourceInitializedPublisher which publishes an event when certain beans have been registered. This bean is added by the HibernateJpaAutoConfiguration class. This class tries to create a LocalContainerEntityManagerFactory however in your configuration you are creating it yourself. Leading to early firing of events.

    Excluding the HibernateJpaAutoConfiguration should fix the problem as that disable the registration of the DataSourceInitializedPublisher.

    @EnableAutoConfiguration(exclude=org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration.class)
    

    Regarding your configuration you can almost remove all of it and simply put some properties in the application.properties files.

    Your WebAppConfig.

    @Configuration
    public class WebAppConfig  extends WebMvcConfigurerAdapter {
    
        @Bean(name = "messageSource")
        public MessageSource configureMessageSource() {
            AbstractMessageSource messageSource = new Dictionary();
            return messageSource;
        }
    
        @Bean
        public LocaleResolver localeResolver() {
            SessionLocaleResolver lr = new SessionLocaleResolver();
            lr.setDefaultLocale(Locale.ENGLISH);
            return lr;
        }
    
        @Bean
        public LocaleChangeInterceptor localeChangeInterceptor(){
            LocaleChangeInterceptor localeChangeInterceptor=new LocaleChangeInterceptor();
            localeChangeInterceptor.setParamName("lang");
            return localeChangeInterceptor;
        }
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(localeChangeInterceptor());
        }
    
    }
    

    And in the application.properties add the following 2 properties

    spring.view.prefix=/WEB-INF/jsp/
    spring.view.suffix=.jsp
    

    Now your RootConfig.

    @Configuration
    public class RootConfig {
    
        @Bean
        public HibernateJpaSessionFactoryBean sessionFactory(EntityManagerFactory emf) {
            HibernateJpaSessionFactoryBean sessionFactoryBean = new HibernateJpaSessionFactoryBean();
            sessionFactoryBean.setEntityManagerFactory(emf);
            return sessionFactoryBean;
        }
    }
    

    And add the following properties to your application.properties

    spring.datasource.url=
    spring.datasource.username=
    spring.datasource.password=
    
    spring.jpa.database-platform= // Hibernate Dialect
    spring.jpa.show-sql=
    spring.jpa.hibernate.ddl-auto=
    spring.jpa.properties.hibernate.format_sql=true
    

    For more properties see the reference guide.

    Your Initializer can be removed as that doesn't do anything in a Spring Boot application. I might have missed a couple of properties but this should give you a start and feel for what Spring Boot can do for you.

    Note: I'm not sure why you need both JPA and plain hibernate if you can do it with JPA only you can simply remove your RootConfig completely.

    0 讨论(0)
提交回复
热议问题