Spring(DI)_01_05

2025. 1. 21. 21:10·Spring, Boot/기초 내용 정리

2025-01-21

 

 

 

 

INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [applicationContextCollection.xml]
INFO : org.springframework.context.support.GenericXmlApplicationContext - Refreshing org.springframework.context.support.GenericXmlApplicationContext@517cd4b: startup date [Tue Jan 21 21:05:28 KST 2025]; root of context hierarchy
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@e4487af: defining beans [collection]; root of factory hierarchy
홍길동님 점수 : 91
세종대왕님 점수 : 100
유관순님 점수 : 93
김유신님 점수 : 77
신사임당님 점수 : 82

INFO : org.springframework.context.support.GenericXmlApplicationContext - Closing org.springframework.context.support.GenericXmlApplicationContext@517cd4b: startup date [Tue Jan 21 21:05:28 KST 2025]; root of context hierarchy
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@e4487af: defining beans [collection]; root of factory hierarchy

 

package com.spring.collection;

import java.util.Map;
import java.util.Set;

import org.springframework.context.support.GenericXmlApplicationContext;

public class CollectionUser {

	public static void main(String[] args) {
		// 스프링 컨테이너를 구동을 하자
		GenericXmlApplicationContext container = 
				new GenericXmlApplicationContext("applicationContextCollection.xml");
		
		// 컨테이너로부터 사용할 객체를 lookup하자
		CollectionBean bean = container.getBean("collection", CollectionBean.class);
		/* CollectionBean bean = (CollectionBean)container.getBean("collection"); 위와 同*/ 
		
		Map<String, Integer> addressList = bean.getAddressList();
		
		for (String str : addressList.keySet()) {
			System.out.println(str + "님 점수 : " + addressList.get(str));
		}
		System.out.println();
		
		// 컨테이너를 종료해 주자
		container.close();
	}
}

 

package com.spring.collection;

import java.util.Map;
import java.util.Set;

public class CollectionBean {

	private Map<String, Integer> addressList;
	

	public Map<String, Integer> getAddressList() {
		return addressList;
	}

	public void setAddressList(Map<String, Integer> addressList) {
		this.addressList = addressList;
	}
 }

 

<?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:p="http://www.springframework.org/schema/p"
	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-3.1.xsd">
	
	<!-- 스프링 컨테이너가 생성할 객체를 bean으로 등록을 한다. -->
	<!-- id : 컨테이너가 생성한 객체들을 식별하기 위해서 사용하는 이름(각각의 고유의 id 지정)
			  자바의 식별자 작성 규칙이 적용이 됨.
			  * 숫자로 시작하면 안됨
			  * 공백을 포함하면 안됨
			  * 특수기호를 사용하면 안됨
		 class : 패키지 경로가 포함된 클래스의 이름을 등록을 함.
	-->
		
	<!-- 컬렉션에서 배열과 List 자료구조를 이용 시 주입 방법 -->
	<bean id="collection" class="com.spring.collection.CollectionBean">
		<property name="addressList">
			<map>
				<entry>
					<key><value>홍길동</value></key>
					<value>91</value>
				</entry>
				
				<entry>
					<key><value>세종대왕</value></key>
					<value>100</value>
				</entry>
				
				<entry>
					<key><value>유관순</value></key>
					<value>93</value>
				</entry>
				
				<entry>
					<key><value>김유신</value></key>
					<value>77</value>
				</entry>
				
				<entry>
					<key><value>신사임당</value></key>
					<value>82</value>
				</entry>
			</map>
		</property>
	</bean>
	<!-- 객체의 변경이 잦은 경우는 bean 태그를 이용함. 또한 유지보수 측면에서
		 변경이 잦지 않은 경우는 애노테이션 사용 -->

</beans>

'Spring, Boot > 기초 내용 정리' 카테고리의 다른 글

Spring(DI)_02_02  (0) 2025.01.21
Spring(DI)_02_01  (0) 2025.01.21
Spring(DI)_01_04  (0) 2025.01.21
Spring(DI)_01_03  (0) 2025.01.21
Spring(DI)_01_02  (0) 2025.01.21
'Spring, Boot/기초 내용 정리' 카테고리의 다른 글
  • Spring(DI)_02_02
  • Spring(DI)_02_01
  • Spring(DI)_01_04
  • Spring(DI)_01_03
mw41817
mw41817
일생의 개발 기록 저장소
  • mw41817
    IT 개발 일지
    mw41817
    • Index (487)
      • HTML (36)
        • 기초 내용 정리 (36)
      • CSS (29)
        • 기초 내용 정리 (29)
      • JavaScript (60)
        • 기초 내용 정리 (60)
      • JQuery (38)
        • 기초 내용 정리 (38)
      • Java (232)
        • 기초 내용 정리 (232)
      • JSP (46)
        • 기초 내용 정리 (46)
      • Spring, Boot (31)
        • 기초 내용 정리 (31)
      • DB (5)
        • Oracle SQL (5)
      • Code WorkBook (6)
        • programmers (6)
        • Baekjoon (0)
      • 기타 (1)
        • 유용한 사이트 (3)
  • 전체
    오늘
    어제
  • 글쓰기 관리
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
    • 카테고리
    • 주인장 GitHub
  • 공지사항

  • 인기 글

  • 태그

    html #코딩 #프로그래밍 #기초
  • 최근 댓글

  • hELLO· Designed By정상우.v4.10.0
mw41817
Spring(DI)_01_05
상단으로

티스토리툴바