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

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.

主站蜘蛛池模板: 乐亭县| 明水县| 沙河市| 双江| 南康市| 凌海市| 旺苍县| 天气| 上饶县| 岗巴县| 绥芬河市| 汕头市| 钟祥市| 加查县| 临城县| 安图县| 海宁市| 九江县| 平度市| 玉树县| 应用必备| 延津县| 嵊州市| 宁陕县| 巴马| 北辰区| 延边| 南投县| 和静县| 泾源县| 思南县| 珠海市| 宜城市| 萝北县| 兰溪市| 娱乐| 德江县| 崇明县| 长武县| 武宁县| 博乐市|