Spring Boot入門から精通まで-初見注釈、restインターフェース
2799 ワード
前の節では簡単なSpring Bootプロジェクトを構築しました.このセクションでは、プロジェクトによってSpring Bootでよく使われている注釈を初歩的に理解します.
まずスタートクラスの同じクラスのディレクトリの下でcontrollerディレクトリを新規作成し、controlerディレクトリの中でjava類を新規作成します.DemoController.java
DemoController.javaでは注釈を利用して簡単なインターフェースを実現します.
詳細な注釈については、ここをクリックしてGetMappingのソースコードを調べます.
以前のspringプロジェクトの中で、@Controllerまたは@Request Mappingだけを注釈しました.この時Springはまだ知らないです.このjavaクラスは、xmlにスキャンパケット経路を配置するか、またはxmlに単独に配置することによって、Spring Bootでは完全にこのステップを免除する必要があります.Spring Bootはデフォルトスキャンスタートクラスの同じクラスのディレクトリにあるすべてのファイルですので、ここでは他のxml構成を必要とせずに直接インターフェースにアクセスできます.
あなたの関心は私の最大の動力です.
まずスタートクラスの同じクラスのディレクトリの下でcontrollerディレクトリを新規作成し、controlerディレクトリの中でjava類を新規作成します.DemoController.java
DemoController.javaでは注釈を利用して簡単なインターフェースを実現します.
@RestController
public class DemoController {
@GetMapping("/test")
public String test() {
return "test";
}
}
このクラスには二つの注釈が使われています.@RertControllerと@GetMapping.プロジェクトを起動して、ブラウザに入力します.http://localhost:8080/test ブラウザからtest文字が出力されます.コードを解析します.RestitControllerのソースコードを調べます.@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
@AliasFor(
annotation = Controller.class
)
String value() default "";
}
RetsControllerソースからRertControllerの注釈が見えます.@Controllerと@ResponseBody.このインターフェースはデータを返すことができる.詳細な注釈については、ここをクリックしてGetMappingのソースコードを調べます.
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(
method = {RequestMethod.GET}
)
public @interface GetMapping {
...
GetMappingソースからRequest Mappingが表示されます.そしてgetタイプが与えられました.つまり、@GetMappingの役割は@Request Mappingに相当し、さらに簡潔である.以前のspringプロジェクトの中で、@Controllerまたは@Request Mappingだけを注釈しました.この時Springはまだ知らないです.このjavaクラスは、xmlにスキャンパケット経路を配置するか、またはxmlに単独に配置することによって、Spring Bootでは完全にこのステップを免除する必要があります.Spring Bootはデフォルトスキャンスタートクラスの同じクラスのディレクトリにあるすべてのファイルですので、ここでは他のxml構成を必要とせずに直接インターフェースにアクセスできます.
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
プロジェクトのスタートクラスでは、ideaがデフォルトで私たちに注釈を加えていることを発見しました.@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
...
私たちはまずSpring BootAppleication上の@ComponentScanに注目します.この注釈はプロジェクト起動後のspringスキャンパッケージの経路です.もしspringがプロジェクト起動時にグローバルをスキャンすることを望まないなら、スタートクラスでspringのスキャン経路を設定するために使用できます.スキャンの範囲を狭めると、プロジェクトの立ち上げ時間が短縮されます.@SpringBootApplication
@ComponentScan(basePackages={"com.example.demo.controller"})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
このセクションでは、いくつかのSpring Bootでよく使われている注釈とrestインターフェースが書かれていることが分かりました.次のセクションではSpring bootの注釈を詳しく分析します.Spring Bootは入門から精通までです.あなたの関心は私の最大の動力です.