서버

Spring - Application Context xml로 bean 설정해보기(옛날 버전)

탐훈 2024. 11. 18. 12:34
728x90

 


SpringBoot에서는 어노테이션만 선언하면 

Bean을 자동으로 ApplicationContext에 Load해준다. 

 

이전에 Spring에서는

Bean을 XML로 직접 ApplicationContext에 실어야 했다.

 

그 과정을 실습하며 느껴보자

 

 

 


[XmlConfigurationContextLauncherApplication.java]

package com.in28minutes.learn_spring_framework.h1;

import java.util.Arrays;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.in28minutes.learn_spring_framework.game.GameRunner;

@Configuration
@ComponentScan
public class XmlConfigurationContextLauncherApplication {
	public static void main(String[] args) {
    	// ClassPathXmlApplicationContext: xml에 Application Context 있어~ 파일명은 이거야
        
		try (var context = new ClassPathXmlApplicationContext("contextConfiguration.xml")) {
			// context에 있는 모든 Bean을 가져와서 출력
            Arrays.stream(context.getBeanDefinitionNames()).forEach(System.out::println);
			// 그 중 GameRunner 가져오기 
			System.out.println(context.getBean("gameRunner"));
			
			context.getBean(GameRunner.class).run();
		}
	}
}

 

[resouces/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"     
 	xmlns:util="http://www.springframework.org/schema/util" 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="age" class="java.lang.Integer">
		<constructor-arg value="35"/>
	</bean>
	
	<context:component-scan base-package="com.in28minutes.learn_spring_framework_scope"/>
	
	<bean id="game" class="com.in28minutes.learn_spring_framework.game.PackManGame"/>
	<bean id="gameRunner" class="com.in28minutes.learn_spring_framework.game.GameRunner">
		<constructor-arg ref="game"></constructor-arg>
	</bean>
</beans>

 

 

[PackManGame.java]

package com.in28minutes.learn_spring_framework.game;

import org.springframework.stereotype.Component;

public class PackManGame implements GamingConsole{

	@Override
	public void up() {
		System.out.println("PackMan: up");
	}

	@Override
	public void down() {
		System.out.println("PackMan: down");
	}

	@Override
	public void left() {
		System.out.println("PackMan: left");
	}

	@Override
	public void right() {
		System.out.println("PackMan: right");
	}

}

 

[GamingConsole.java]

package com.in28minutes.learn_spring_framework.game;

public interface GamingConsole {
	void up();
	void down();
	void left();
	void right();
}

 

[GameRunner.java]

package com.in28minutes.learn_spring_framework.game;

public class GameRunner {
	private GamingConsole game;
	
	public GameRunner(GamingConsole game) {
		this.game = game;
	}

	public void run() {
		System.out.println("Running game: " + game);
		game.up();
		game.down();
		game.left();
		game.right();
	}
}

 

bean id 중

'game'과 'gameRunner'는

class에 패키지명 + 파일명으로 선언해놓으면

해당 패키지에 있는 파일이 bean으로 설정된다. 

 

main 메소드를 실행해보면 아래와 같이 나온다.

.

 

 

Spring의 어노테이션과

xml로 Bean 생성하는 방식의 차이는

 

1) 디버깅 차이(어노테이션은 디버깅이 어려움)

2) 관리 포인트 차이(xml설정시 수정이 여러 곳에서 이루어짐)

3) POJO 여부(xml 사용시 일반적인 자바 파일이다. 어노테이션은 보이지 않는 추가 설정이 붙음)