Creating an application class

Under src/main/kotlin/{packageName}, create an application class named SpringBootKotlinApplication.kt:

@SpringBootApplication
class SpringBootKotlinApplication

fun main(args: Array<String>) {
runApplication<SpringBootKotlinApplication>(*args)
}

@SpringBootApplication is utilized to empower the following three features:

  • @Configuration enables Java-based configuration.
  • @EnableAutoConfiguration enables the auto-configuration feature of SpringBoot.
  • @ComponentScan enables component scanning.

The main() function utilizes SpringBoot's SpringApplication.run() method to dispatch an application. This web application is 100% unadulterated Kotlin and there's no need to arrange any pipes or foundations here.

Similarly, there's a CommandLineRunner function set apart as @Bean and this keeps running on startup. It recovers every one of the beans that were made either by your application or were naturally added by SpringBoot. It then sorts and prints these out.

In the code of the SpringBootKotlinApplication class, in contrast with Java, you can see the absence of semicolons, the absence of sections in an empty class (you can add a few, in case you have to proclaim beans by means of a @Bean annotation), and the utilization of a runApplication top-level function. runApplication<SpringBootKotlinApplication>(*args) is Kotlin's informal option in contrast to SpringApplication.run(SpringBootKotlinApplication::class.java, *args), and this can be utilized to customize the application.

Now create an HTML file in the folder underneath src/main/resources/templates/

The content of index.html is as follows:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>Spring Boot Kotlin</title>
</head>
<body>
<p>Welcome, Naruto. This project is based on Spring Boot in Kotlin</p>
</body>
</html>

Start the web application by running the main function of SpringBootKotlinApplication.kt. If everything is fine, you'll see this in the logcat:

Next, go to http://localhost:8080/. Here, you should see a web page with a SpringBoot Kotlin application headline:

We've covered the basics of SpringBoot. Later, we'll go into this in more depth with more dependencies.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.87.161