2021. 6. 28. 13:45ㆍDev/Java
#Annotation
Annotation = 메타데이터 => 컴파일/런타임 과정에서 해당 코드를 어떻게 처리할 것인지를 알려주는 정보
예를 들면 @Getter의 경우는 컴파일/런타임 시 자동으로 get필드 함수를 CamelCase의 형태로 만들어준다.
#주요 Annotation
@ComponentScan
@Component, @Service, @Repository, @Controller, @Configuration 이 붙은 Bean을 찾아서 등록
@Component
개발자가 직접 작성한 class를 Bean으로 등록하기 위한 Annotation
@Component
//@Component(value = "myStudent) -> Bean의 id를 value로 등록, 미지정시 camelCase로
public class Student {
public Student() {
System.out.println("hi");
}
}
@Bean
개발자가 제어 불가능한 외부 라이브러리등을 Bean으로 등록할 때.
반드시 @Configuration과 같이 와야함
@Configuration
public class A {
@Bean
// @Bean을 사용하기 위해서는 해당 객체를 반환하는 Method를 지정해주면 됨
// ArrayList<String>은 라이브러리이기 때문에 개발자가 제어 못해서 @Bean을 사용!!
public ArrayList<String> array() {
return new ArrayList<String>();
}
}
@Autowired (= @Inject)
의존성 주입을 할 때 사용 = 의존 객체 타입 Bean을 찾아 주입한다.
스프링 4.3부터는 생략 가능함
//생성자에 @Autowired
public class A {
private B b;
//타입 B인 필드변수 b에 대입, 만약 B가 Bean으로 등록되어 있지 않다면 에러!!
@Autowired
public A(B b) {
this.b = b
}
}
//Setter
@필드에 Autowired
public class A {
@Autowired
B b;
}
@SpringBootApplication
스프링 부트의 가장 기본적인 설정 선언 => @ComponentScan @Configuration @EnableAutoConfiguration을 기본 포함
//패키징 해놔야함!!
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
@Service
서비스 클래스에서 사용 => 비즈니스 로직을 수행하는 Class라는 것을 나타냄
@Repository
DataBase에 접근하는 클래스에 적용
@Configuration
@AutoWired로 Bean을 부를 수 있게 해주는 어노테이션
#REST Annotation & Controller Annotation
@Controller
Spring의 Controller
@RestController = @Controller + @ResponseBody
반환 결과를 JSON 형태로 반환하는 클래스 => HttpResponse로 바로 응답이 가능
@RequestMapping
요청 URL을 어떤 method가 처리할 지 mapping, 형식 정의 안하면 자동으로 GET
@RestController
@RequestMapping("/users")
public class UserController {
//...logic
//여기서 리턴되는 method는 JSON으로 리턴됨
}
@CrossOrigin
CORS 뚫어주는 어노테이션
@RestController
@CrossOrigin(origins = "*") //모든 도메인 허용
@RequestMapping("/users")
public class UserController {
//...logic
}
@...Mapping
"Method" + Mapping = 해당 Http Method로 사용가능하게 함
@GetMapping("/") //"RequestMapping 도메인 + /"로 Get 요청시 실행
@PostMapping("/") //Post 요청
@DeleteMapping("/") //Delete 요청
@PutMapping("/{id}") //Put 요청 => @ApiParam으로 해당 id 값 가져오기 가능
#Lombok
@NoArgsConstructor
기본 생성자 자동 추가
@NoArgsConstructor
public class A {
private Integer field1;
private boolean field2;
private String field3;
}
//아래와 같은 코드임
public class A {
private Integer field1;
private boolean field2;
private String field3;
//기본 생성자
public A() {};
}
@AllArgsConstructor
모든 필드 값을 파라미터로 받는 생성자 추가
@AllArgsConstructor
public class A {
private Integer field1;
private boolean field2;
private String field3;
}
//아래와 같은 코드임
public class A {
private Integer field1;
private boolean field2;
private String field3;
//모든 필드 값 받는 생성자
public A(Integer field1, boolean field2, String field3) {
this.field1 = field1;
this.field2 = field2;
this.field3 = field3;
};
}
@RequiredArgsConstructor
final 필드 값만 파라미터로 받는 생성자
@RequiredArgsConstructor
public class A {
private final B b;
private final C c;
}
@Getter, Setter
각각 필드의 값을 get, set 할 수 있는 method 추가
@Getter
@Setter
public class A {
private String field;
}
//아래와 같은 코드
public class A {
private String field;
public String getField() {
return this.field;
}
public void setField(String field) {
this.field = field;
}
}
Reference
https://velog.io/@gillog/Spring-Annotation-정리
[Spring] Annotation 정리
Annotation(@)은 사전적 의미로는 주석이라는 뜻이다. 자바에서 사용될 때의 Annotation은 코드 사이에 주석처럼 쓰여서 특별한 의미, 기능을 수행하도록 하는 기술이다.
velog.io
'Dev > Java' 카테고리의 다른 글
JAVA : Stream? (0) | 2021.08.31 |
---|---|
#Spring Structure- Controller, Service, Domain(Model), DTO, Persistance 알아보기 (0) | 2021.08.22 |
Abstract Class , Interface (0) | 2021.06.26 |
Java의 기본 개념 정리 (0) | 2021.06.26 |