Java/Spring

[Spring] 스프링 빈 등록하는 방법

뉴벡엔드 2024. 3. 27. 14:53

컴포넌트 스캔

 

@Component 이있으면 스프링 컨테이너에 자동 등록 

 

@Controller, @Service, @Repository 는 @Component가 포함되어 있어 스프링 컨테이너에 자동으로 등록됨.

 

 

@Autowired 를 사용하여  스프링과 연관된 객체를 컨테이너에서 찾아서 주입해줌(Dependency Injection : 의존성 주입). 


자바 코드로 등록

 

@Configuration
public class SpringConfig {

    @Bean
    public TestService testService() {
        return new TestService(testRepository());
    }

    @Bean
    public TestRepository testRepository() {
        return new TestRepository();
    }
}

 

반응형

'Java > Spring' 카테고리의 다른 글

[Spring]Model 객체와 @ModelAttribute  (0) 2024.04.02
[Spring]MariaDB 연동  (0) 2024.03.28
[Spring]AOP  (0) 2024.03.28
[Spring]JdbcTemplate  (0) 2024.03.28
[Spring]window에서 bulid 후 실행  (0) 2024.03.27