Skip to content

Commit

Permalink
Switch from spring.factories to AutoConfiguration.imports (#1636)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronny Bräunlich committed Sep 11, 2023
1 parent 4b5b04a commit 920a671
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ object Versions {
const val sqlserver = "9.4.1.jre8"

/** Spring **/
const val springFramework = "5.3.22"
const val springBoot = "2.7.2"
const val springFramework = "6.0.11"
const val springBoot = "3.1.3"

/** Test Dependencies **/
const val testContainers = "1.17.3"
Expand Down
19 changes: 15 additions & 4 deletions exposed-spring-boot-starter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,24 @@ Example:

```kotlin
@Configuration
@EnableAutoConfiguration(exclude = [DataSourceTransactionManagerAutoConfiguration::class])
class ExposedConfig {
@Bean
fun databaseConfig() = DatabaseConfig {
useNestedTransactions = true
}
@Bean
fun databaseConfig() = DatabaseConfig {
useNestedTransactions = true
}
}
```
It is recommended that the `DataSourceTransactionManagerAutoConfiguration` class be excluded from auto-configuration, which can be done in a custom configuration, as shown above.

Alternatively, the class can be disabled using the `exclude` attribute of `@SpringBootApplication`:

```kotlin
@SpringBootApplication(exclude = [DataSourceTransactionManagerAutoConfiguration::class])
class MyApplication
```

See the [official documentation](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using.auto-configuration.disabling-specific) for more options to exclude auto-configuration classes.

## Automatic Schema Creation
This starter will create the database schema if enabled automatically using any class that extends `org.jetbrains.exposed.sql.Table`
Expand Down
2 changes: 1 addition & 1 deletion exposed-spring-boot-starter/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dependencies {
testImplementation("org.springframework.boot", "spring-boot-starter-test", Versions.springBoot)
// put in testImplementation so no hard dependency for those using the starter
testImplementation("org.springframework.boot", "spring-boot-starter-webflux", Versions.springBoot)
testImplementation("com.h2database", "h2", Versions.h2)
testImplementation("com.h2database", "h2", Versions.h2_v2)
}

tasks.withType<Test>().configureEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.jetbrains.exposed.spring.DatabaseInitializer
import org.jetbrains.exposed.spring.SpringTransactionManager
import org.jetbrains.exposed.sql.DatabaseConfig
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.autoconfigure.AutoConfiguration
import org.springframework.boot.autoconfigure.AutoConfigureAfter
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
Expand All @@ -14,8 +15,7 @@ import org.springframework.context.annotation.Configuration
import org.springframework.transaction.annotation.EnableTransactionManagement
import javax.sql.DataSource

@Configuration
@AutoConfigureAfter(DataSourceAutoConfiguration::class)
@AutoConfiguration(after = [DataSourceAutoConfiguration::class])
@EnableTransactionManagement
open class ExposedAutoConfiguration(private val applicationContext: ApplicationContext) {

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.jetbrains.exposed.spring.autoconfigure.ExposedAutoConfiguration
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package org.jetbrains.exposed.spring

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration
import org.springframework.scheduling.annotation.EnableAsync

@EnableAsync
@SpringBootApplication
@SpringBootApplication(exclude = [DataSourceTransactionManagerAutoConfiguration::class])
open class Application {

open fun main(args: Array<String>) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jetbrains.exposed.spring.autoconfigure

import org.jetbrains.exposed.spring.Application
import org.jetbrains.exposed.spring.DatabaseInitializer
import org.jetbrains.exposed.spring.SpringTransactionManager
import org.jetbrains.exposed.spring.tables.TestTable
Expand All @@ -9,9 +10,13 @@ import org.jetbrains.exposed.sql.selectAll
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.NoSuchBeanDefinitionException
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.AutoConfigurations
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.TestConfiguration
import org.springframework.boot.test.context.runner.ApplicationContextRunner
import org.springframework.context.annotation.Bean
import org.springframework.scheduling.annotation.Async
import org.springframework.stereotype.Service
Expand Down Expand Up @@ -50,6 +55,18 @@ open class ExposedAutoConfigurationTest {
assertEquals(expectedConfig.maxEntitiesToStoreInCachePerEntity, databaseConfig!!.maxEntitiesToStoreInCachePerEntity)
}

@Test
fun testClassExcludedFromAutoConfig() {
val contextRunner = ApplicationContextRunner().withConfiguration(
AutoConfigurations.of(Application::class.java)
)
contextRunner.run { context ->
assertThrows(NoSuchBeanDefinitionException::class.java) {
context.getBean(DataSourceTransactionManagerAutoConfiguration::class.java)
}
}
}

@TestConfiguration
open class CustomDatabaseConfigConfiguration {

Expand Down

0 comments on commit 920a671

Please sign in to comment.