지난 글에서는 @Component와 그 하위 Annotation(Service/Controller/Repository)를 이용한 Bean 등록에 관한 내용을 다뤘는데 이번 글에서는 Bean을 등록할 수 있는 다른 방법에 대해서 알아볼 것이다.
package com.test.service;
public class ReturnByeService {
public String ReturnBye() {
return "Bye Bye!";
}
}
package com.test.service;
public class ReturnGoodService {
public String ReturnGood() {
return "Good Job!";
}
}
위와 같이 해당 클래스에는 @Service Annotation을 붙이지 않고 생성한 평범한 Java Class이다.
package com.test.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.test.service.*;
@Configuration
public class TestConfig {
@Bean
public ReturnByeService ReturnByeService() {
return new ReturnByeService();
}
@Bean
public ReturnGoodService ReturnGoodService() {
return new ReturnGoodService();
}
}
별도의 클래스 파일에 @Configuration Annotation을 붙이고, 해당 클래스 내에 특정 객체를 반환하는 함수를 정의하고, @Bean Annotation을 붙여 빈으로 등록할 수 있다.
'공부 > Spring' 카테고리의 다른 글
[Spring] Maven? Gradle? (0) | 2022.02.23 |
---|---|
[Spring] Spring vs SpringBoot (0) | 2022.02.22 |
[Spring] DI(의존성 주입) 3가지 방법 (0) | 2022.02.21 |
[Spring] 기본적인 @AutoWired Annotation 사용법 (0) | 2022.02.21 |
[Spring] Spring Framework 정리 (0) | 2022.02.21 |