XML Configuration π οΈ
XMLμ μ¬μ©ν΄ Beanμ μ€μ νλ κ²μ Annotationμ΄ μλ μμ μ μ¬μ©λμΌλ©° μ΅κ·Όμλ κ±°μ μ¬μ©νμ§ μλλ€.
νμ§λ§ Legacy μ½λλ₯Ό 보기 μν΄ λ³Ό μλ μμΌλ μ΄ν΄κ° νμνλ€.
XML Configuration μμ μ½λ π
Java μ½λ
package com.hippoo.studyspring.examples.h1;
import com.hippoo.studyspring.game.GameRunner;
import java.util.Arrays;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class XmlConfigurationContextLauncherApplication {
public static void main(String[] args) {
try (var context = new ClassPathXmlApplicationContext("contextConfiguration.xml")) {
Arrays.stream(context.getBeanDefinitionNames()).forEach(System.out::println);
System.out.println(context.getBean("name"));
System.out.println(context.getBean("age"));
context.getBean(GameRunner.class).run();
}
}
}
XML μ€μ νμΌ
μμΉ: resources/contextConfiguration.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="name" class="java.lang.String">
<constructor-arg value="Ranga"/>
</bean>
<bean id="age" class="java.lang.Integer">
<constructor-arg value="35"/>
</bean>
<context:component-scan base-package="com.hippoo.studyspring.game"/>
<bean id="game" class="com.hippoo.studyspring.game.PacmanGame"/>
<bean id="gameRunner" class="com.hippoo.studyspring.game.GameRunner">
<constructor-arg ref="game" />
</bean>
</beans>
μ€λͺ π
XML νμΌμ ν΅ν΄ μ€νλ§ λΉμ μ€μ ν μ μμΌλ©°, <bean> νκ·Έλ₯Ό μ¬μ©νμ¬ λΉμ μμ±νλ€. μ΄λ @Bean, @Component λ±μ μ΄λ
Έν
μ΄μ
μ μ¬μ©νλ κ²κ³Ό λμΌν μν μ νλ©°, μμ±μ μΈμλ‘ κ°μ²΄λ₯Ό λκΈΈ λλ ref μ΅μ
μ μΆκ°νμ¬ μμ‘΄μ±μ μ£Όμ
νλ€.
μμ μλ @Configuration, @Component, @Beanκ³Ό κ°μ μ΄λ
Έν
μ΄μ
μ΄ μμκΈ° λλ¬Έμ XMLλ‘ λͺ¨λ μ€μ μ νλ€. μ΅κ·Όμ λ§λ€μ΄μ§ μ½λλ€μ μ€μ μ μ΄λ
Έν
μ΄μ
μΌλ‘ μ£Όλ‘ μ¬μ©νμ§λ§, μ¬μ ν κΈ°μ‘΄μ XML μ€μ μ μ¬μ©νλ νλ‘μ νΈλ€μ΄ λ§κΈ° λλ¬Έμ μ΄λ₯Ό μ΄ν΄κ° νμνλ€.
μ΄λ Έν μ΄μ κ³Ό λΉκ΅ν π
μλ νλ XML μ€μ κ³Ό μ΄λ Έν μ΄μ κΈ°λ° μ€μ μ μ£Όμ μ°¨μ΄μ μ λΉκ΅ν κ².
| νλͺ© | XML μ€μ | μ΄λ Έν μ΄μ κΈ°λ° μ€μ |
|---|---|---|
| μ€μ λ°©μ | XML νμΌμ λΉ μ μ | μ½λ λ΄ μ΄λ Έν μ΄μ μ¬μ© |
| κ°λ μ± | μ€μ νμΌμ΄ κΈΈμ΄μ§ μ μμ | μ½λμ μ€μ μ΄ λ°μ νκ² μ°λλμ΄ κ°λ μ±μ΄ μ’μ |
| μ μ§λ³΄μμ± | μ€μ λ³κ²½ μ XML νμΌ μμ νμ | μ½λ λ³κ²½κ³Ό λμμ μ€μ λ λ³κ²½ κ°λ₯ |
| μμ‘΄μ± μ£Όμ | refλ value μμ± μ¬μ© | μμ±μ, νλ, μΈν°λ₯Ό ν΅ν μ£Όμ |
| μ μ°μ± | λ€μν μ€μ μ XMLμμ μ½κ² κ΄λ¦¬ κ°λ₯ | μ΄λ Έν μ΄μ μ ν΅ν΄ λμ μΌλ‘ μ€μ κ°λ₯ |
| λλ²κΉ | XML μ€μ μ€λ₯ μ λ°νμμμ λ°κ²¬ κ°λ₯ | μ»΄νμΌ νμμ μΌλΆ μ€λ₯ λ°κ²¬ κ°λ₯ |
| νμ΅ κ³‘μ | XML λ¬Έλ²μ λν μ΄ν΄ νμ | μλ° μ΄λ Έν μ΄μ μ λν μ΄ν΄ νμ |
| νκ²½ μ€μ λΆλ¦¬ | μ€μ κ³Ό μ½λκ° λͺ νν λΆλ¦¬λ¨ | μ€μ κ³Ό μ½λκ° ν¨κ» μ‘΄μ¬ν¨ |
Pros & Cons βοΈ
Pros β
- μμ μλ°λ‘ POJO μ μ§: XML μ€μ μ μμ μλ° κ°μ²΄λ₯Ό μ μ§ν μ μμ΄ ν μ€νΈκ° μ©μ΄ν©λλ€.
- λλ²κΉ μ΄ μ‘°κΈ λ μ½λ€: μ΄λ Έν μ΄μ μ κ²½μ° μ€νλ§ νλ μμν¬κ° μμμ μ€μ μ μ²λ¦¬νκΈ° λλ¬Έμ νλ μμν¬μ λμμ κΉμ΄ μ΄ν΄ν΄μΌ νμ§λ§, XML μ€μ μ μ€μ νμΌμ μ§μ λ³΄κ³ μ΄ν΄ν μ μμ΅λλ€.
Cons β
- μ μ§λ³΄μκ° νλ€λ€: XML μ€μ νμΌμ΄ 컀μ§μλ‘ κ΄λ¦¬νκΈ° μ΄λ €μμ§λλ€.
- μ₯ν©νλ€: μ΄λ Έν μ΄μ μ λΉν΄ μ€μ μ΄ κΈΈμ΄μ Έ μ½λκ° μ₯ν©ν΄μ§ μ μμ΅λλ€.
TIP π‘
Annotationκ³Ό XML μ€ νλλ§ μ¬μ©ν΄μ μ€μ μ μΌκ΄μ± μκ² μ μ§. λ κ°μ§ λ°©μμ νΌμ©νλ©΄ μ€μ μ 볡μ‘μ±μ΄ μ¦κ°νκ³ μ μ§λ³΄μκ° μ΄λ €μμ§ μ μλ€.