[springcore] 빈 설정 방법

빈 설정 방법

XML 설정 (고전적)

<!-- xml 설정파일 -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

	<bean id="bookService" class="com.juho.springapplicationcontext.BookService">
	</bean>

	<bean id="bookRepository" class="com.juho.springapplicationcontext.BookRepository">
	</bean>
</bean>

… 생략

```java
@Service
public class BookService {
    BookRepository bookRepository;

    public void setBookRepository(BookRepository bookRepository) {
        this.bookRepository = bookRepository;
    }
}

… 생략


#### 자바 클래스 설정
- `@Configuration`을 붙이면 자바 객체도 설정파일로 사용할 수 있음
```java
@Configuration
public class ApplicationConfig {
    @Bean
    public BookRepository bookRepository() {
        return new BookRepository();
    }

    @Bean
    public BookService bookService() {
        BookService bookService = new BookService();
        bookService.setBookRepository(bookRepository());
        return bookService;
    }
}
  • 해당 설정파일을 사용하려면
    public class DemoApplication {
      public static void main(String[] args) {
          ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
    				
          String[] beanDefinitionNames = context.getBeanDefinitionNames();
          System.out.println(Arrays.toString(beanDefinitionNames));
    
          BookService bookService = (BookService) context.getBean("bookService");
          System.out.println(bookService.bookRepository != null);
      }
    }
    
  • 자바 객체에도 애노테이션을 사용하여 componentScan을 할 수 있음
    @Configuration
    @ComponentScan(basePackageClasses = DemoApplication.class)
    public class ApplicationConfig {
      ...생략
    
  • 특정 패키지를 지정할 수도 있지만 위와 같이 클래스를 지정하면 해당 클래스 하위에 있는 빈들을 등록시킨다는 의미