
Annotation이란 method나 class 위에 @가 붙은 텍스트를 말한다. Method나 class 위에 붙은 annotation은 특별한 기능을 수행하게 된다. 즉, 프로그램에게 추가적인 정보를 제공해주는 메타데이터라 볼 수 있다.
메타 데이터(meta data): 데이터를 위한 데이터
Spring Framework은 Spring MVC 패턴을 따르며 각 모듈별로 사용하는 annotation이 상이하다. 간단하게 Controller, Model, Repository, Service에서 자주 쓰이는 annotation을 (매우 간단하게...)정리하고자 한다.
❗ 꼭 작성된 모듈에서
annotation을 사용하지 않아도 됩니다.
@Controller를 알아야 한다. 둘의 결정적인 차이는 HTTP Response Body가 생성되는 방식의 차이다.@Controller는 아래와 같은 과정을 통해 주로 View를 반환할 때 사용한다.// 예시
@RequestMapping("/list"), @RequestMapping("/home, /about")
@RequestMapping("/admin", method=RequestMehod.GET)@RequestMapping(Method=ReqeustMethod.GET)과 같음@PostMapping, @PutMapping, @PatchMapping, @DeleteMapping은 유추 가능하므로 생략예시)
UserTable→user_table
Auto_Increment를 사용하기 위해서는 GenerationType.IDENTITY를 사용하며 다음과 같이 코드를 작성한다.@GeneratedValue(strategy = GenerationType.{option})
기본값으로 사용된다.// 예시
@OneToMany(mappedBy = "{Table name}", fetch=FetchType.{option})
//예시
@JoinTale(
name = "{Table name}",
joinColumes = @JoinColumn(name = "{Attribute name}"),
inverseJoinColumns = @JoiColumn(name = "{Attribute name}"))
JPA가 어떠한 Entity를 불러올 때 이 Entity와 관계된 Entity를 불러올지에 대한 정보를 제공한다.
EntityGraph는 Fetch와 Load 두가지 타입으로 나뉜다.
FETCH :entity graph에 명시된 attribute는 FetchType.EAGER이고 명시되지 않은 attribute는 FetchType.LAZY이다.
LOAD : entity graph에 명시된 attribute는 FetchType.EAGER이고 명시되지 않은 attribute는 명시되어 있는 FetchType이나 기본값인 FetchType이다.
//예시
@EntityGraph(attributePaths = {"Entity Name"})
//예시
@Query("Query")
해당 어노테이션이 붙어잇는 메인 어플리케이션 클래스의 위치를 프로젝트 디폴트 패키지의 루트에 위치시키는 것을 추천한다.
@Configuration, @EnableAutoConfiguration, @ComponentScan 3가지를 하나의 어노테이션으로 합친 것이다.
@NotNull이 붙은 필드에 대해 생성자(Constructor)를 만들어 준다.한마디로 로그 남길 때 쓰인다.dependency 객체의 Type에 해당하는 Bean을 찾아 주입한다.Contructor, Setter, Field와 같이 3가지 경우에 사용되며 기본값으로 true이기 때문에 Dependency Injection할 대상을 찾지 못한다면 어플리케이션 구동에 실패한다.@Compoenent는 개발자가 직접 작성한 Class를 Bean으로 등록하기 위한 Annotation이다.
@Component
public class Hello{
public Hello(){
System.out.println("Hello World");
}
}
@Component(value="mtstudent")
public class Hello{
public Hello(){
System.out.println("Hello World");
}
}
Component에 대한 추가 정보가 없다면 Class의 이름을 CamelCase로 변경하는 것이 Bean id로 사용된다.
하지만 @Bean과 다르게 @Component는 name이 아닌 value를 이용해 Bean의 이름을 지정한다.
Configuration을 클래스에 적용하고 @Bean을 해당 클래스의 메소드에 적용하면 @Autowired로 Bean을 부를 수 있다.
Bean을 정의할 때 사용한다.