Backend/Spring

'XML 기반' VS 'Java Config 기반'

해로몬 2024. 11. 15. 17:09

1. XML 기반 설정

XML 기반 설정은 스프링에서 전통적으로 사용되어 온 방식으로, XML 파일을 통해 빈을 정의하고 의존성을 주입합니다. applicationContext.xml과 같은 설정 파일에서 <bean> 태그를 사용하여 빈을 설정합니다.

XML 설정 예제 (applicationContext.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>">

    <!-- Bean 정의 -->
    <bean id="helloService" class="org.example.HelloService">
        <property name="message" value="안녕하세요, 스프링!" />
    </bean>

    <bean id="writeAction" class="org.example.WriteAction">
        <property name="helloService" ref="helloService" />
    </bean>
</beans>

XML 설정 설명

  • <bean id="helloService" class="org.example.HelloService">: HelloService라는 클래스를 빈으로 정의하고, message라는 프로퍼티를 설정합니다.
  • <bean id="writeAction" class="org.example.WriteAction">: WriteAction이라는 클래스에서 helloService 빈을 주입받습니다.

XML 설정의 특징

  • 명시적 구성: XML 파일을 통해 모든 빈과 의존성을 명시적으로 구성하므로, 빈의 설정과 구조가 한눈에 파악되기 쉽습니다.
  • 외부 설정 파일 관리 용이: XML 파일을 별도로 관리하므로 코드와 설정이 분리됩니다.

2. Java Config 기반 설정

Java Config 방식은 자바 클래스를 사용하여 스프링 빈과 의존성을 설정하는 방식입니다. 클래스에 @Configuration 어노테이션을 사용하고, 빈으로 등록할 메서드에 @Bean 어노테이션을 적용합니다.

Java Config 예제 (AppConfig.java)

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

@Configuration
public class AppConfig {

    @Bean
    public HelloService helloService() {
        HelloService helloService = new HelloService();
        helloService.setMessage("안녕하세요, 스프링!");
        return helloService;
    }

    @Bean
    public WriteAction writeAction() {
        WriteAction writeAction = new WriteAction();
        writeAction.setHelloService(helloService());
        return writeAction;
    }
}

Java Config 설정 설명

  • @Configuration: 이 클래스를 스프링 설정 파일로 사용함을 나타냅니다.
  • @Bean: 해당 메서드의 리턴값을 빈으로 등록합니다.
  • helloService() 메서드: HelloService 클래스의 빈을 정의하고, message 프로퍼티를 설정합니다.
  • writeAction() 메서드: WriteAction 클래스의 빈을 정의하고 helloService 빈을 주입합니다.

Java Config 설정의 특징

  • 타입 안전성: XML에서 문자열로 빈을 참조하는 방식과 달리, Java Config는 컴파일 타임에 타입을 체크할 수 있습니다.
  • IDE 지원: 메서드 이름과 코드 자동 완성 등의 IDE 기능을 사용할 수 있어, 작성과 유지보수가 편리합니다.
  • 유연성: 자바 코드이기 때문에 복잡한 설정이나 조건문 등을 활용한 동적 설정이 가능합니다.

3. XML 설정과 Java Config 설정 비교

설정방법 XML 기반 설정 Java Config 기반 설정
설정 위치 applicationContext.xml 등의 XML 파일 @Configuration 클래스 내 @Bean 메서드
타입 안전성 없음 (문자열 참조) 있음 (컴파일 시점에 타입 체크 가능)
설정의 유연성 상대적으로 제한적 자바 코드의 유연성을 활용한 동적 설정 가능
IDE 지원 제한적 (자동 완성 기능 지원 미약) 메서드 자동 완성, 코드 리팩토링 등의 IDE 지원
장점 설정과 코드가 분리되어 가독성 및 관리 용이 코드 작성과 테스트에 유리한 구조 제공

 

 

>>애플리케이션 실행 예제

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public static void main(String[] args) {
    //1.XML 기반
    	ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        WriteAction action = context.getBean("writeAction", WriteAction.class);
        action.execute();
    
    //2.Java Config파일로 로드
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        WriteAction action = context.getBean("writeAction", WriteAction.class);
        action.execute();
    }
}

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

@Autowired  (1) 2024.11.15
Bean Scope  (0) 2024.11.15
Spring Life Cycle⭐️  (0) 2024.11.15
IoC(Inversion of Control) : 제어의 역전  (0) 2024.11.14
라이브러리와 프레임워크  (0) 2024.11.14