- Kotlin Blueprints
- Ashish Belagali Hardik Trivedi Akshay Chordiya
- 257字
- 2021-07-02 21:50:17
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.
- 編程的修煉
- Building Modern Web Applications Using Angular
- CentOS 7 Linux Server Cookbook(Second Edition)
- Python Tools for Visual Studio
- Animate CC二維動畫設計與制作(微課版)
- Python王者歸來
- Scala編程實戰(原書第2版)
- Go并發編程實戰
- UVM實戰
- Scala Data Analysis Cookbook
- Java語言程序設計教程
- 時空數據建模及其應用
- Visual Basic程序設計習題與上機實踐
- uni-app跨平臺開發與應用從入門到實踐
- 深入解析Java編譯器:源碼剖析與實例詳解