- 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.
- 精通Nginx(第2版)
- Mastering macOS Programming
- The HTML and CSS Workshop
- The Complete Coding Interview Guide in Java
- 單片機C語言程序設計實訓100例
- Learning Concurrency in Kotlin
- INSTANT Yii 1.1 Application Development Starter
- IPython Interactive Computing and Visualization Cookbook
- HTML5移動Web開發
- ROS機器人編程實戰
- Android技術內幕(系統卷)
- Practical Linux Security Cookbook
- Splunk Developer's Guide(Second Edition)
- Java EE框架開發技術與案例教程
- C#程序開發教程