본문 바로가기

Programing/Spring FrameWork

[Spring] Bean 주입방법 비교(xml vs Java)

728x90
반응형

[Spring] Bean 주입방법 비교(xml vs Java)


ApplicationContext 

1) xml ( ClassPathXmlApplicationContext )

-> XML  파일에 Bean을 각각 등록하는 방법

-> property를 활용하여 의존관계 주입

 

2) Java ( AnnotationConfigApplicationContext )

-> Annotation을 활용하여 Bean을 등록하는 방법

-> Autowired 어노테이션을 활용하여 의존관계 주입

 

* SpringBoot에서는 @SpringBootApplication 어노테이션을 사용.


1) xml파일로 Bean 주입하기(고전방법)

 

application.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	// bookService Bean 등록 -> bookRepository의 의존관계 주입
	// property의 name값은 bookService의 setter값이며 ref값은 참조할 bean의 id값이다.
    <bean id="bookService" class="me.whiteship.springapplicationcontext.BookService" >
           <property name="bookRepository" ref="bookRepository" />
    </bean>

	// bookRepository Bean 등록
    <bean id="bookRepository" class="me.whiteship.springapplicationcontext.BookRepository" />

</beans>

 

BookService.class
package me.whiteship.springapplicationcontext;

public class BookService {

    BookRepository bookRepository;

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

}

 

BookRepository.class
package me.whiteship.springapplicationcontext;

public class BookRepository {
}

 

DemoApplication.class
package me.whiteship.springapplicationcontext;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Arrays;

public class DemoApplication {

    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
        String[] beanDefinitionNames = context.getBeanDefinitionNames();
        System.out.println(Arrays.toString(beanDefinitionNames));
        BookService bookService = (BookService) context.getBean("bookService");
        System.out.println(bookService.bookRepository != null);

    }

}

 

실행결과

 

xml configuration file로 Bean을 등록하였을때 단점:  Bean으로 하나씩 등록하는 것이 번거롭다.


2) Java파일로 Bean 주입하기

application.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 https://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="me.whiteship.springapplicationcontext" />

</beans>

 

BookService.class
package me.whiteship.springapplicationcontext;

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

@Service
public class BookService {

	@Autowired
    BookRepository bookRepository;

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

}

 

BookRepository.class
package me.whiteship.springapplicationcontext;

import org.springframework.stereotype.Repository;

@Repository
public class BookRepository {
}

 

ApplicationConfig.class
package me.whiteship.springapplicationcontext;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ApplicationConfig {

    @Bean
    public BookRepository bookRepository(){
        return new BookRepository();
    }
    
    @Bean
    public BookService bookService(){
        BookService bookService = new BookService();
        bookService.setBookRepository(bookRepository());
        return bookService;
    }
    
}

 

DemoApplication.class
package me.whiteship.springapplicationcontext;

import org.springframework.context.ApplicationContext;

import java.util.Arrays;

public class DemoApplication {

    public static void main(String[] args) {

        ApplicationContext context = new AnnotationConfigAppContext(ApplicationConfig.class);
        String[] beanDefinitionNames = context.getBeanDefinitionNames();
        System.out.println(Arrays.toString(beanDefinitionNames));
        BookService bookService = (BookService) context.getBean("bookService");
        System.out.println(bookService.bookRepository != null);

    }

}

번외) SpringBoot Bean 주입방법

DemoApplication.class
package me.whiteship.springapplicationcontext;

import org.springframework.boot.autoconfigure.SpringBootApplication;

// @SpringBootApplication가 ComponentScan과 Configuraiton을 포함한다.
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {

    }

}

 

728x90
반응형