- 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.
- Advanced Quantitative Finance with C++
- Advanced Splunk
- Mastering Entity Framework Core 2.0
- Java入門經(jīng)典(第6版)
- PHP 從入門到項(xiàng)目實(shí)踐(超值版)
- Maven Build Customization
- 認(rèn)識(shí)編程:以Python語言講透編程的本質(zhì)
- Full-Stack Vue.js 2 and Laravel 5
- MATLAB定量決策五大類問題
- Troubleshooting PostgreSQL
- Java應(yīng)用開發(fā)技術(shù)實(shí)例教程
- Learning Apache Mahout Classification
- 深入實(shí)踐DDD:以DSL驅(qū)動(dòng)復(fù)雜軟件開發(fā)
- Solr權(quán)威指南(下卷)
- Wearable:Tech Projects with the Raspberry Pi Zero