'spring beans'에 해당되는 글 1건

# XML로 bean 생성하기

    -  pom.xml : 스프링의 버전 정의 

    -  Main.java : 생성된 Bean 객체 가져오기

    -  beans.xml : 생성할 bean 객체 명시

    -  TestBean.java : 생성할 Bean 객체

 

# Spring Bean의 알아야할 점

  -  Bean으로 생성된 객체는 싱글톤이다. 싱글톤이라는 의미는 한번 생성되면, 계속해서 똑같은 객체를 불러온다.

     이점은 아래의 생성 과정에서 알아보도록 하겠습니다.

 

 


1. pom.xml

  -  Spring의 버전은 5.2.0.RELEASE 을 사용하도록 하겠습니다.

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>minami.tistory.test</groupId>
	<artifactId>TistoryTest</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<description></description>

	<properties>
		<!-- 자바 버전 -->
		<java-version>1.8</java-version>
		<!-- 스프링 버전 -->
		<org.springframework-version>5.2.0.RELEASE</org.springframework-version>
		<!-- 스프링 버전의 변경이 자유롭게 -->
		<!-- <org.springframework-version>4.3.25.RELEASE</org.springframework-version> -->
		<org.slf4j-version>1.7.28</org.slf4j-version>
		<ch.qos.logback-version>1.2.3</ch.qos.logback-version>
	</properties>

	<!-- 프로젝트에서 사용할 라이브러리 정보 -->
	<dependencies>
		<!-- Spring Context -->
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${org.slf4j-version}</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>${ch.qos.logback-version}</version>
			<exclusions>
				<exclusion>
					<groupId>org.slf4j</groupId>
					<artifactId>slf4-api</artifactId>
				</exclusion>
			</exclusions>
			<scope>runtime</scope>
		</dependency>
	</dependencies>
</project>

 

 

2. TestBean.java (package beans;)

  -  TestBean의 생성자를 만들어서 생성 시점을 알아보겠습니다.

  -  생성자를 만들어서 TestBean.java의 생성 시점을 확인해보겠습니다.

package beans;

public class TestBean {
	// 생성자
	public TestBean() {
		System.out.println("---------------------");
		System.out.println("TestBean을 생성하였습니다.");
		System.out.println("---------------------");
	}
}

 

 

3. config.xml (package config;)

  -  TestBean을 정의하겠습니다.

     id : Main.java에서 bean을 가져올 때, 이름 지정.

     class : 생성할 객체의 위치.

<?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">

	<!-- 총 두개의 bean을 생성합니다. -->
	<!-- id값 없이 생성해 보도록 하겠습니다. -->
	<bean class="beans.TestBean"></bean>
	
	<!-- bean id 값으로 객체를 불러올 거라 bean id 값을 부여하였습니다. -->
	<bean id="test1" class="beans.TestBean"></bean>	

</beans>

4. Main.java(package config;)

  -  TestBean을 정의하겠습니다.

     id : Main.java에서 bean을 가져올 때, 이름 지정.

     class : 생성할 객체의 위치.

package main;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import beans.TestBean;

public class Main {

	public static void main(String[] args) {
		System.out.println("-1------------");
		System.out.println("TestBean 두번 생성되는 이유는? id가 있는 경우, id가 없는 경우 총 두번 생성");
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("config/beans.xml");
		
		System.out.println("-2------------");
		// beans.xml에서 정의한 TestBean의 요소를 가져오겠습니다.
		System.out.println("-3--getBean 불러오기 전-");
		
		// getBean해서 부르기 전에 이미 TestBean은 생성이 되어있는 상황.		
		TestBean t1 = ctx.getBean("test1", TestBean.class);
		System.out.printf("t1 : %s\n", t1);
		
		TestBean t2 = ctx.getBean("test1", TestBean.class);
		System.out.printf("t2 : %s\n", t2);
		
		System.out.println("t1 , t2의 주소가 동일하게 나옴.");
		// t1과 t2의 주소가 동일하게 나올 것입니다.
		
		System.out.println("-4--getBean 불러온 후-");
		ctx.close();
	}

}

4. 출력화면

  -  Main.java을 유심하게 보여 출력 내용을 이해할 수 있습니다.

  - t1, t2의 주소값이 같은 이유는 Spring 한번 생성하면 싱글톤으로 유지하기 때문에, getBean해도 같은 주소를 가리킵니다.

  - bean 정의된 요소들의 생성 시점 getBean한 시점으로 옮겨 보도록 하겠습니다.

 


5. 객체의 싱글톤, 생성 시점의 변경해보자.

  -  객체의 생성 시점이 getBean한 시점으로 변경하는 방법. (XML에서 scope)

  -  객체의 생성이 항상 동일한 객체가 아니 getBean 할 때 새로운 객체 생성하는 방법(XML에서 lazy-init)

  -> 다음 강좌에서 이어집니다.

블로그 이미지

미나미나미

,