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

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.

主站蜘蛛池模板: 靖宇县| 武川县| 漳浦县| 桐柏县| 漳州市| 延庆县| 仁怀市| 瑞昌市| 始兴县| 肇州县| 岚皋县| 巫山县| 文山县| 昔阳县| 松溪县| 利川市| 都匀市| 西乡县| 峨边| 塘沽区| 饶河县| 南皮县| 深州市| 邛崃市| 南阳市| 丹阳市| 高邮市| 台湾省| 舒兰市| 诸暨市| 冕宁县| 城口县| 孝感市| 虹口区| 合川市| 万载县| 泉州市| 辛集市| 汉阴县| 铁岭县| 姚安县|