官术网_书友最值得收藏!

Application class

We define our  SpringApplication class to launch our Spring Boot application from the Kotlin code. It reads the application configuration from application.yml in our case, but supports being read from various sources.

We use @SpringBootApplication to launch our application as a Spring Boot application and @EnableTransactionManagement to enable database transaction support for databases:

    /**
* To launch Spring Boot application and hold the application
level properties
*/
@SpringBootApplication
@EnableTransactionManagement
class Application {

/**
* To deserialize-serialize the PostGIS data structures
*/
@Bean
fun objectMapper(): ObjectMapper =
Jackson2ObjectMapperBuilder()
.modulesToInstall(PostGISModule())

.serializationInclusion(JsonInclude.Include.NON_NULL)
.build()


/**
* Configuring transaction support
*/
@Bean
fun transactionManager(@Qualifier("dataSource") dataSource:
DataSource) = SpringTransactionManager(dataSource)

/**
* Initialize our web app each time it runs
*/
@Bean
fun init(mr: MessageRepository) = CommandLineRunner {
mr.createTable()
mr.deleteAll()
}
}

/**
* Launch the Spring Boot application
*/
fun main(args: Array<String>) {
SpringApplication.run(Application::class.java, *args)
}

To configure transaction support with the database we simply need to specify the @EnableTransactionManagement annotation and pass PlatformTransactionManager with the transactionManager bean using the Application class:

    @Bean
fun transactionManager(@Qualifier("dataSource") dataSource:
DataSource) = SpringTransactionManager(dataSource)

Moreover, we need to configure our object mapper (Jackson) with an additional module for mapping PostGIS data structures: 

    @Bean
fun objectMapper(): ObjectMapper =
Jackson2ObjectMapperBuilder()
.modulesToInstall(PostGISModule())
.serializationInclusion(JsonInclude.Include.NON_NULL)
.build()

You can specify your main function for JVM outside the  Application class, such functions that are not part of any class are called Top-level functions in Kotlin and they come from the functional side of Kotlin.

主站蜘蛛池模板: 杂多县| 河曲县| 哈密市| 正蓝旗| 西藏| 呼玛县| 固安县| 光山县| 屏东县| 磐石市| 泸州市| 舟曲县| 商南县| 循化| 南投市| 通城县| 佛坪县| 库车县| 香河县| 崇阳县| 罗定市| 阳曲县| 嫩江县| 南丰县| 邵东县| 天门市| 乾安县| 满洲里市| 石城县| 晋州市| 唐海县| 桑日县| 洱源县| 奇台县| 绵阳市| 京山县| 东阳市| 石台县| 喀喇| 驻马店市| 如皋市|