- Hands-On Microservices with Kotlin
- Juan Antonio Medina Iglesias
- 212字
- 2021-06-30 19:10:56
Understanding the component scan
Let's go back to our example that we created previously:
package com.microservices.chapter2
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.ResponseBody
@SpringBootApplication
class Chapter2Application
@Controller
class FirstController(val exampleService: ExampleService) {
@RequestMapping(value = "/user/{name}", method = arrayOf(RequestMethod.GET))
@ResponseBody
fun hello(@PathVariable name: String) = exampleService.getHello(name)
}
fun main(args: Array<String>) {
runApplication<Chapter2Application>(*args)
}
We add a controller class, then when our application starts we can see in the log:
RequestMappingHandlerMapping : Mapped "{[/user/{name}],methods=[GET]}" onto public java.lang.String com.microservices.chapter2.FirstController.hello(java.lang.String)
How has the application found our controller and wired into a request mapping?
When the Spring Boot application starts, it will scan all the classes and packages underneath the application context class recursively, and if any class is annotated as a component it will create an instance of it and add it to the Spring Boot application context. This feature is named as the component scan.
Later on, when the Spring Boot application starts, if it's a web application, it will get any class annotated with @Controller that is on the context and create a mapping that will get the request coming to our microservice to that class.
- UI設計基礎培訓教程
- AngularJS入門與進階
- React.js Essentials
- WordPress Plugin Development Cookbook(Second Edition)
- Visual FoxPro程序設計
- Apache Kafka Quick Start Guide
- Salesforce Reporting and Dashboards
- Unity 2D Game Development Cookbook
- Java面向對象程序設計
- Access 2010中文版項目教程
- Python Essentials
- HTML+CSS+JavaScript網頁設計從入門到精通 (清華社"視頻大講堂"大系·網絡開發視頻大講堂)
- Mastering OpenStack
- 分布式數據庫HBase案例教程
- Java面向對象程序設計教程