定义在Hibernate SessionFactory之前启动的Micronaut bean

时间:2018-10-05 18:36:48

标签: micronaut

我想使用Flyway迁移数据库。在Grails(春季)应用程序中,可以简单地定义一个自定义bean并根据该bean标记Hibernate SessionFactory。

flyway(Flyway) { bean ->
    bean.initMethod = 'migrate'
    dataSource = ref('dataSource')
    locations = 'classpath:migration/db/mysql'
    baselineOnMigrate = true
    table = 'schema_version'
}

if (springConfig.containsBean('hibernateDatastore')) {

    BeanDefinition beanToInlineWithFlyway = getBeanDefinition('hibernateDatastore')

    def dependsOnList = ['flyway'] as Set
    if (beanToInlineWithFlyway.dependsOn?.length > 0) {
        dependsOnList.addAll(beanToInlineWithFlyway.dependsOn)
    }
    beanToInlineWithFlyway.dependsOn = dependsOnList as String[]
}

Micronaut中有没有办法做到这一点?

我喜欢这样做的原因是:

使用Flyway进行数据库迁移时,您将所有DDL内容委托给Flyway并将Hibernate jpa.default.properties.hibernate.hbm2dll.auto设置为validate

jpa:
  default:
    properties:
      hibernate:
        hbm2ddl:
          auto: validate

Flyway将在Hibernate之前启动,并将检查是否需要迁移数据库表。之后,Hibernate启动,并针对当前的JPA实体验证数据库模式。

1 个答案:

答案 0 :(得分:1)

您可以定义实现fun openSpeechRecognitionSettings() { val intent = Intent(Intent.ACTION_MAIN) intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK val components = arrayOf(ComponentName("com.google.android.googlequicksearchbox", "com.google.android.apps.gsa.settingsui.VoiceSearchPreferences"), ComponentName("com.google.android.voicesearch", "com.google.android.voicesearch.VoiceSearchPreferences"), ComponentName("com.google.android.googlequicksearchbox", "com.google.android.voicesearch.VoiceSearchPreferences"), ComponentName("com.google.android.googlequicksearchbox", "com.google.android.apps.gsa.velvet.ui.settings.VoiceSearchPreferences")) for (componentName in components) { try { intent.component = componentName startActivity(intent) break } catch (e: Exception) { TODO("handle me") } } } 的bean,而不必依赖脆弱的bean名称。该操作将在创建BeanCreatedEventListener<DataSource> bean之后但在创建DataSource之前运行。