Spring Boot和Hibernate。处理会话工厂

时间:2017-09-27 13:17:34

标签: java hibernate spring-mvc spring-boot kotlin

我遇到了Spring和hibernate会话的问题。我在配置类中有以下bean:

@Bean
fun sessionFactory(@Autowired entityManagerFactory: EntityManagerFactory) :
        SessionFactory = entityManagerFactory.unwrap(SessionFactory::class.java)

但它会导致应用程序无法启动。

The dependencies of some of the beans in the application context form a cycle:

   userRepository defined in file [/home/m/Code/enterprise/out/production/classes/com/xxx/site/domain/user/repository/UserRepository.class]
┌─────┐
|  getSessionFactory defined in class path resource [com/xxx/site/WebMvcConfig.class]
└─────┘

用户存储库:

@Repository
@Qualifier("mysql")
class UserRepository(@Autowired private val sessionFactory: SessionFactory): UserRepositoryInterface{
...
}

我使用Spring boot 2.0.0.M4。

虽然Spring Boot 1.5.4.RELEASE没有出现此问题。

任何想法该怎么做?

修改 SiteApplication.kt

package com.xxx.site

import com.xxx.site.interceptor.CustomLocaleChangeInterceptor
import nz.net.ultraq.thymeleaf.LayoutDialect
import org.hibernate.SessionFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.web.servlet.LocaleResolver
import org.springframework.web.servlet.config.annotation.InterceptorRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
import org.springframework.web.servlet.i18n.SessionLocaleResolver
import java.util.*
import javax.persistence.EntityManagerFactory

@SpringBootApplication
class SiteApplication


@Configuration
class WebMvcConfig: WebMvcConfigurer {

    @Bean
    fun localeResolver(): LocaleResolver {
        val slr = SessionLocaleResolver()
        slr.setDefaultLocale(Locale("th"))
        return slr
    }

    override fun addInterceptors(registry: InterceptorRegistry?) {
        registry!!.addInterceptor(CustomLocaleChangeInterceptor()).addPathPatterns("/**")
    }

    @Bean
    fun layoutDialect(): LayoutDialect {
        return LayoutDialect()
    }

    @Bean
    fun sessionFactory(@Autowired entityManagerFactory: EntityManagerFactory) :
            SessionFactory = entityManagerFactory.unwrap(SessionFactory::class.java)

}


fun main(args: Array<String>) {
    SpringApplication.run(SiteApplication::class.java, *args)
}

0 个答案:

没有答案