Spring - Spring Bean Configuration File

yeong ·2023년 2월 13일
0

spring

목록 보기
6/8
<?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">
	<!-- Spring Bean Configuration File를 여러개 작성하여 사용하는 이유 -->
	<!-- => 하나의 Spring Bean Configuration File을 사용하여 Spring Bean를 설정할 경우 가독성
	및 유지보수의 비효율성 증가 -->

	<!-- import : 다른 Spring Bean Configuration File의 Sprng Bean 정보를 제공받아 포함하기 위한 엘리먼트 -->
	<!-- resource 속성 : 포함될 Spring Bean Configuration File의 경로를 속성값으로 설정 -->
	<!-- <import resource="03_message.xml"/> -->
	
	<!-- bean : 스프링 컨테이너에게 Spring Bean 관련 정보를 제공하기 위한 엘리먼트 -->
	<!-- => Spring Bean : 스프링 컨테이너의 의해 관리(생성,사용,소멸)되는 객체(클래스) -->
	<!-- class 속성 : Spring Bean으로 등록되어 사용될 클래스를 속성값으로 설정 - 필수 -->
	<!-- => class 속성값은 이클립스의 자동 완성 기능을 사용하여 작성 가능 -->
	<!-- id 속성 : Spring Bean를 구분하기 위한 식별자를 속성값으로 설정 -->
	<!-- => id 속성 대신 name 속성을 사용하여 식별자 설정 가능 -->
	<!-- => id 속성값은 클래스 이름(부모 인터페이스 이름)을 이용하여 설정하는 것을 권장 -->
	<!-- => id 속성값은 class 속성이 설정되어 있는 경우 이클립스의 자동 완성 기능을 사용하여 작성 가능 -->
	<bean class="xyz.itwill04.bean.CreateBean" id="createBean"/>
</beans>
<?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">

	<!-- init-method 속성 : 스프링 컨테이너에 의해 객체(Spring Bean)가 생성된 후 한번만 자동
	호출되어 객체의 초기화 작업을 실행하기 위한 메소드의 이름을 속성값으로 설정 -->
	<!-- => init-method 속성값은 이클립스의 자동 완성 기능을 사용하여 작성 가능 -->
	<!-- destroy-method 속성 : 스프링 컨테이너에 의해 객체(Spring Bean)가 소멸되기 전 한번만 
	자동 호출되어 객체의 마무리 작업을 실행하기 위한 메소드의 이름을 속성값으로 설정 -->
	<!-- => destroy-method 속성값은 이클립스의 자동 완성 기능을 사용하여 작성 가능 -->
	<bean class="xyz.itwill04.bean.InitDestroyMethodBean" id="initDestroyMethodBean"
		init-method="init" destroy-method="destroy"/>

	<!-- lazy-init 속성 : false 또는 true 중 하나를 속성값으로 설정 -->
	<!-- => false(기본) : 스프링 컨테이너를 초기화할 때 객체(Spring Bean)를 미리 생성  -->
	<!-- => true : 스프링 컨테이너로부터 Spring Bean를 제공받기 위해 getBean() 메소드를 호출할 때 객체 생성 -->
	<bean class="xyz.itwill04.bean.LazyInitBean" id="lazyInitBean" lazy-init="true"/>
	
	<!-- 스프링 컨테이너는 Spring Bean Configuration File에 등록된 모든 클래스를 리플렉션
	기술을 사용하여 미리 객체(Spring Bean)로 생성 -->
	<!-- => 리플렉션 기술을 사용하면 클래스의 접근 지정자에 상관없이 모든 요소에 접근 가능 -->
	<!-- => 생성자가 은닉화 선언되어 있어도 스프링 컨테이너는 클래스의 생성자로 객체 생성 -->
	<!-- Spring Bean Configuration File에 등록된 싱글톤 클래스는 클래스가 메모리에 로딩된 후
	정적영역의 명령을 실행하여 객체를 생성하고 스프링 컨테이너에 의해 객체를 다시 생성 -->
	<!-- => 싱글톤 클래스에 의해 객체가 2개 생성 - 싱글톤 클래스의 작성 규칙 위반 -->
	<!-- factory-method 속성 : 싱글톤 클래스에서 객체를 반환하는 메소드의 이름을 속성값으로 설정 -->
	<!-- => 스프링 컨테이너에 의해 객체를 생성하지 않고 정적영역의 명령으로 객체를 생성하여 사용 -->
	<bean class="xyz.itwill04.bean.FactoryMehodBean" factory-method="getFactoryMehodBean"/>
	
	<!-- 스프링 컨테이너틑 bean 엘리먼트의 선언 순서대로 등록된 클래스를 객체로 생성 -->
	<!-- depends-on 속성 : Spring Bean를 구분하기 위한 식별자(beanName)를 속성값으로 설정 -->
	<!-- => bean 엘리먼트에 등록된 클래스를 객체로 생성하기 전에 depends-on 속성값으로 설정된 
	Spring Bean의 클래스를 객체로 미리 생성 -->
	<bean class="xyz.itwill04.bean.DependsOnOneBean" depends-on="dependsOnTwoBean"/>
	<bean class="xyz.itwill04.bean.DependsOnTwoBean" id="dependsOnTwoBean"/>
	 
	<!-- scope 속성 : singleton(기본), prototype, request, session 중 하나를 속성값으로 설정 -->
	<!-- singleton 또는 prototype : 객체(Spring Bean)의 생성 갯수를 설정하는 속성값 -->
	<!-- => singleton : 스프링 컨테이너가 bean 엘리먼트에 등록된 클래스로 객체를 하나만 생성하여 제공 -->
	<!-- => prototype : 스프링 컨테이너가 bean 엘리먼트에 등록된 클래스로 객체를 여러개 생성하여 제공 -->
	<!-- => scope 속성값을 [prototype]으로 설정할 경우 lazy-init 속성값을 반드시 [true]로 설정 -->
	<!-- request 또는 session : 객체의 사용범위를 설정하는 속성값 - 웹프로그램 작성시에만 사용 -->
	<bean class="xyz.itwill04.bean.ScopeBean" id="singletonBean" lazy-init="true" scope="singleton"/>
	<bean class="xyz.itwill04.bean.ScopeBean" id="prototypeBean" lazy-init="true" scope="prototype"/>
</beans>
<?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 class="xyz.itwill04.bean.AnnotationBean" id="annotationBean"/> -->
	
	<!-- component-scan : 스프링 어노테이션(Spring Annotation)을 스프링 컨테이너가 검색하여
	처리할 수 있도록 설정하는 엘리먼트 -->
	<!-- => context 네임스페이스의 spring-context.xsd 파일에 의해 제공되는 엘리먼트 -->
	<!-- base-package 속성 : 스프링 컨테이너가 스프링 어노테이션을 사용한 클래스를 검색하기
	위한 패키지를 속성값으로 설정 -->
	<context:component-scan base-package="xyz.itwill04.bean"/>
</beans>
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- Spring Bean으로 등록된 클래스의 기본 생성자를 이용하여 객체 생성 -->
	<!-- => 객체의 필드에는 기본값(숫자형  :0, 논리형 : false, 참조형 : null) -->
	<bean class="xyz.itwill05.di.Student" id="student1"/>
	 
	 <!-- Spring Bean 으로 등록된 클래스의 매개변수가 선언도니 생성자를 이용하여 객체 생성  -->
	 <!-- => bean 엘리먼트의 하위 엘리먼트를 사용하여 생성자 매개변수의 값을 전달하여 필드값으로 -->
	 <!-- => Constructor Injection : 생성자를 이용하여 객체 필드 초기화 작업 실행 -->
	 <bean class="xyz.itwill05.di.Student" id="student2">
	 	<!-- constructor-arg :  Spring Bean 으로 등록된 클래스의 매개변수에 값(객체)을 
	 	전달하기 위한 엘리먼트 -->
	 	<!-- => 엘리먼트의 갯수만큼 매개변수가 선언된 생성자가 반드시 작성 -->
	 	<!-- value 속성 : 매개변수에 전달하기 위해 값을 속성값으로 설정  -->
	 	<!-- => Spring Bean 등록된 클래스가 객체로 생성될 때  필드에 전달값 저장 -->
	 	<!-- => Value Injection : 객체의 필드에 값이 저장되도록 초기화 작업 실행 -->
	 	<!-- => 전달값은 기본적으로 문자열로 (String 객체)로 전달 -매개변수에 자료형에 의해 자동 형변환  -->
	 	<!-- => 매개변수의 자료형에 의해 자동 형변환될 경우 NunFormatException 발생 가능  -->
	 	<constructor-arg value="1000"/>
	 </bean>
	 
	 
</beans>

0개의 댓글