Spring/Spring framework

Java 어노테이션과 XML 설정

hoonssss 2023. 8. 25. 23:59
반응형
SMALL
<?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 definitions here -->
	
	<bean id = "name" class = "java.lang.String">
		<constructor-arg value="Ranga"/>
	</bean>
	
	<bean name = "age" class = "java.lang.Integer">
		<constructor-arg value="35"/>	
	</bean>
	
	
	<bean id = "game" class = "com.in28minutes.learnspringframework.Packmangame"/> //패키지.클래스
	
	
	<bean id = "gameRunner" class = "com.in28minutes.learnspringframework.GameRunner">
	 <constructor-arg ref="game"/> //참조 value X ref사용
	</bean>
	
<!-- <context:component-scan base-package="com.in28minutes.learnspringframework"/>   -->
		
</beans>
package com.in28minutes.learnspringframework.examples.h1;

import com.in28minutes.learnspringframework.Packmangame;

import java.util.Arrays;
import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.in28minutes.learnspringframework.GameRunner;
import com.in28minutes.learnspringframework.MarioGame;
import com.in28minutes.learnspringframework.SuperContra;
import com.in28minutes.learnspringframework.packman;

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"));
			
			System.out.println(context.getBean("game"));
			
			System.out.println(context.getBean("gameRunner"));
			
			context.getBean(GameRunner.class).run();
		}
	}
}

출력

name
age
game
gameRunner
Ranga
35
com.in28minutes.learnspringframework.Packmangame@482bce4f
com.in28minutes.learnspringframework.GameRunner@366647c2
Running game : com.in28minutes.learnspringframework.Packmangame@482bce4f
jump
Go into a hole
Go back
Acclerate

Annotations

쉬움

관리가 쉬움

자주사용

 

XML

번거로움

POJO가 깔끔함

관리가 번거로움

요즘 거의 사용 안함

 

섞어서 사용하는건 비추

반응형
LIST

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

Spring 전체구조  (0) 2023.08.26
Spring Stereotype Annotations  (0) 2023.08.26
CDI @Named, @Inject  (0) 2023.08.25
@Lazy, @Scope, @PostConstruct, @PreDestroy  (0) 2023.08.25
@Component, @Bean 비교  (0) 2023.08.21