[Spring Framework] アノテーション

アノテーション同等説明
基本設定
@SpringBootApplication@Configuration + @EnableAutoConfiguration + @ComponentScanIndicates a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning.
@EnableAutoConfigurationEnable auto-configuration of the Spring Application Context, attempting to guess and configure beans that you are likely to need. Auto-configuration classes are usually applied based on your classpath and what beans you have defined.
@ComponentScanConfigures component scanning directives for use with @Configuration classes. Provides support parallel with Spring XML’s <context:component-scan> element.
RESTエンドポイント
@RestController@Controller +
@ResponseBody
Types that carry this annotation are treated as controllers where @RequestMapping methods assume @ResponseBody semantics by default.
リクエスト/
レスポンス
@PostMapping@RequestMapping(method = RequestMethod.POST)Annotation for mapping HTTP POST requests onto specific handler methods.
@GetMapping@RequestMapping(method = RequestMethod.GET)Annotation for mapping HTTP GET requests onto specific handler methods.
@PutMapping@RequestMapping(method = RequestMethod.PUT)Annotation for mapping HTTP PUT requests onto specific handler methods.
@DeleteMapping@RequestMapping(method = RequestMethod.DELETE)Annotation for mapping HTTP DELETE requests onto specific handler methods.
@PathVariableAnnotation which indicates that a method parameter should be bound to a URI template variable.
@RequestParamAnnotation which indicates that a method parameter should be bound to a web request parameter.
@RequestBodyAnnotation indicating a method parameter should be bound to the body of the web request.
コンポーネントタイプ
@ComponentIndicates that an annotated class is a “component”. Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.
Other class-level annotations may be considered as identifying a component as well, typically a special kind of component: e.g. the @Repository annotation or AspectJ’s @Aspect annotation.
@Servicea specialization of @ComponentIndicates that an annotated class is a “Service”, originally defined by Domain-Driven Design (Evans, 2003) as “an operation offered as an interface that stands alone in the model, with no encapsulated state.”
@Repositorya specialization of @ComponentIndicates that an annotated class is a “Repository”, originally defined by Domain-Driven Design (Evans, 2003) as “a mechanism for encapsulating storage, retrieval, and search behavior which emulates a collection of objects”.
@Controllera specialization of @ComponentIndicates that an annotated class is a “Controller” (e.g. a web controller).
This annotation serves as a specialization of @Component, allowing for implementation classes to be autodetected through classpath scanning. It is typically used in combination with annotated handler methods based on the RequestMapping annotation.
@ConfigurationIndicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime.
Beans
@BeanIndicates that a method produces a bean to be managed by the Spring container.
@AutowiredMarks a constructor, field, setter method, or config method as to be autowired by Spring’s dependency injection facilities. This is an alternative to the JSR-330 Inject annotation, adding required-vs-optional semantics.
JPA関連
@EntitySpecifies that the class is an entity.
@IdSpecifies the primary key of an entity.
@GeneratedValueProvides for the specification of generation strategies for the values of primary keys.
テスト
@SpringBootTestAnnotation that can be specified on a test class that runs Spring Boot based tests.
@MockBeanAnnotation that can be used to add mocks to a Spring ApplicationContext.
@ValidatedVariant of JSR-303’s Valid, supporting the specification of validation groups. Designed for convenient use with Spring’s JSR-303 support but not JSR-303 specific.
その他
@ConditionalOnJava@Conditional that matches based on the JVM version the application is running on.
Lombok
@Data@Data is a convenient shortcut annotation that bundles the features of @ToString, @EqualsAndHashCode, @Getter / @Setter and @RequiredArgsConstructor together
@NoArgsConstructor@NoArgsConstructor will generate a constructor with no parameters.
@AllArgsConstructor@AllArgsConstructor generates a constructor with 1 parameter for each field in your class.