@Autowired, @Resource, @Inject는 모두 Spring Framework에서 의존성 주입 (Dependency Injection, DI)을 위한 어노테이션입니다. 이 어노테이션들은 객체 간의 의존성을 자동으로 주입하기 위해 사용되며, 각각의 특징이 다릅니다.
타입 -> 이름 -> @Qualifier -> 실패
@Service
public class MyService {
public void serve() {
System.out.println("MyService is serving...");
}
}
@Controller
public class MyController {
// MyService 타입에 맞는 빈을 자동으로 주입
@Autowired
private MyService myService;
public void execute() {
myService.serve();
}
}
이름 -> 타입 -> @Qualifier -> 실패
@Service("myService") // 빈 이름을 명시적으로 설정
public class MyService {
public void serve() {
System.out.println("MyService is serving...");
}
}
@Controller
public class MyController {
// @Resource는 이름을 기준으로 의존성을 주입
@Resource(name = "myService") // "myService"라는 이름을 가진 빈을 찾아서 주입
private MyService myService;
public void execute() {
myService.serve();
}
}
타입 -> @Qualifier -> 이름 -> 실패
@Service
public class MyService {
public void serve() {
System.out.println("MyService is serving...");
}
}
@Controller
public class MyController {
// @Inject는 타입을 기준으로 의존성을 주입합니다.
@Inject
private MyService myService;
public void execute() {
myService.serve();
}
}
Reference
https://velog.io/@sungmo738/
https://itjava.tistory.com/22