티스토리 뷰



'@Bean' and '@Autowired' annotation in Spring Boot


* Spring Boot 에서 bean 은 객체를 의미한다. 


두 annotation 은 객체를 불러주기에 비슷해 보이지만 큰 차이가 있다. 바로 @Bean annotation은 Class 내에 객체를 먼저 '생성' 시키고 만들어진 객체를 부르는 반면 @Autowired  annotation 은 이미 이전에 '만들어진' 객체를 불러오는 기능만은 담당하기 때문이다. 그래서 객체를 미리 생성하지 않고 @Autowired annotation을 사용한다면 앞서 만들어 놓은 객체가 없기 때문에 에러를 발생시킨다.   





'@Resource' and '@Autowired' 


@Autowire는 Bean을 불러올때 같은 type을 기준으로 불러오지만, @Resource는 name을 기준으로 같은이름의 Bean을 불러온다.

e.g. @Resource(name='"userService") 







reference 

https://stackoverflow.com


'Spring Boot ' 카테고리의 다른 글

Creating Spring Boot application with IntelliJ II  (0) 2019.02.27
Creating Spring Boot application with IntelliJ  (0) 2019.02.27
@RestController  (0) 2019.02.10
Dependency Injection  (0) 2019.01.30
Spring Boot annotation  (0) 2019.01.26