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

  • 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.

主站蜘蛛池模板: 新绛县| 德化县| 肇州县| 即墨市| 清流县| 衢州市| 建湖县| 天水市| 远安县| 信丰县| 徐水县| 高台县| 游戏| 恩平市| 馆陶县| 开封市| 屏东市| 无棣县| 郯城县| 葵青区| 莲花县| 龙州县| 阿勒泰市| 瑞安市| 健康| 淳安县| 金溪县| 驻马店市| 昌都县| 延长县| 改则县| 冕宁县| 清丰县| 甘谷县| 灵山县| 南昌县| 文安县| 城步| 灵武市| 连山| 徐闻县|