Home
Java
Spring @Autowired (스프링 의존 관계 자동 연결)
devfoxstar
devfoxstar
March 30, 2022
1 min

Table Of Contents

01
필드 수준
02
메서드 수준
03
생성자 수준

@Autowired 애너테이션은 빈을 자동으로 연결하는 기능입니다.
@Autowired생성자, 메서드, 필드 수준에서 사용할 수 있습니다.


필드 수준

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SpringTestServiceImpl implements SpringTestService {

    @Autowired
    private SpringTestDao springTestDao;

}

메서드 수준

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SpringTestServiceImpl implements SpringTestService {

    private SpringTestDao springTestDao;

    @Autowired
    public void obtainSpringTestDao (SpringTestDao springTestDao) {
        this.springTestDao = springTestDao;
    }

}

생성자 수준

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SpringTestServiceImpl implements SpringTestService {

    private SpringTestDao springTestDao;

    @Autowired
    public SpringTestServiceImpl (SpringTestDao springTestDao) {
        this.springTestDao = springTestDao;
    }

}

필드, 메서드, 생성자에 설정하면, AutowiredAnnotationBeanPostProcessor를 통해 자동 연결을 수행합니다.


Tags

#Java#Spring#Autowired#Bean

Related Posts

Java - Record class (불변 데이터 객체 만들기)
June 11, 2024
1 min
© 2024, All Rights Reserved.

Quick Links

About Me

Media