봄 @Autowired 종속성 주입 주석을 사용하는 예를 빠르게 살펴보겠습니다.
다음 UserService 클래스가 있다고 가정합니다.
이 클래스는 UserRepository 인터페이스를 사용하여 데이터베이스에서 사용자 정보를 검색하는 기능을 제공합니다.
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public User getUserById(int id) {
return userRepository.getUserById(id);
}
public List<User> getAllUsers() {
return userRepository.getAllUsers();
}
}
위에 사용자 서비스 클래스 @Autowired 메모사용 UserRepository 개체 삽입~에 있다
따라서 UserService 클래스에서 UserRepository 메서드 호출할 수 있어요.
UserService 클래스도 있습니다. @서비스 메모가 있습니다.
이것은 스프링 프레임워크에게 UserService 클래스를 빈으로 등록하라고 지시하는 것입니다.
이렇게 빈으로 등록된 클래스는 해당 클래스의 인스턴스를 다른 클래스에서 사용할 수 있습니다. @Autowired 주석으로 주입받을 수 있다
