SpringBoot 啓動的時候提示 Field *** in *** required a bean named 'entityManagerFactory' that could not be f

 錯誤截圖spring

 

後面發現原來和入口類代碼有關。api

//@SpringBootApplication(scanBasePackages = {"org.jzc.odata.cboard","org.jzc.odata.cboard.odataservice","com.sdl.odata.api.service","com.sdl.odata.api.service.ODataService"})
@EnableAutoConfiguration(exclude = {HibernateJpaAutoConfiguration.class
        , DataSourceAutoConfiguration.class,
        DataSourceTransactionManagerAutoConfiguration.class })
@Configuration
@Import({
        ODataDataSourceConfiguration.class,
        ODataServiceConfiguration.class
})
@ComponentScan
@RestController
@ComponentScan(basePackages = {"org.jzc.odata.cboard","org.jzc.odata.cboard.odataservice","com.sdl.odata.api.service","com.sdl.odata.api.service.ODataService"})
public class ServiceContainer {

其中springboot

@EnableAutoConfiguration(exclude = {HibernateJpaAutoConfiguration.class
        , DataSourceAutoConfiguration.class,
        DataSourceTransactionManagerAutoConfiguration.class })

這段的解釋以下,spa

參考code

https://stackoverflow.com/questions/45350546/entitymanagerfactory-not-found-in-springbootblog

也就是說,不去掉@EnableAutoConfiguration 和裏面exclude 包含對EntityManagerFactory 的註釋的話,EntityManagerFactory是不會被實例化的,這樣確定就不能發現entityManagerFactory的Bean了。it

那麼解決方案就是把這段註釋掉,從新編譯打包io

//@SpringBootApplication(scanBasePackages = {"org.jzc.odata.cboard","org.jzc.odata.cboard.odataservice","com.sdl.odata.api.service","com.sdl.odata.api.service.ODataService"})
//@EnableAutoConfiguration(exclude = {HibernateJpaAutoConfiguration.class
//        , DataSourceAutoConfiguration.class,
//        DataSourceTransactionManagerAutoConfiguration.class })
@Configuration
@Import({
        ODataDataSourceConfiguration.class,
        ODataServiceConfiguration.class
})
@ComponentScan
@RestController
@ComponentScan(basePackages = {"org.jzc.odata.cboard","org.jzc.odata.cboard.odataservice","com.sdl.odata.api.service","com.sdl.odata.api.service.ODataService"})
public class ServiceContainer {

再運行,成功了!編譯

相關文章
相關標籤/搜索