티스토리 뷰
Spring/이론
[Spring] 스프링 빈 설정파일 ( Spring Bean Configuration File ) - applicationContext.xml 예제
printf100 2020. 3. 18. 21:041. src 우클릭 > com.test01 패키지 생성
2. com.test01 우클릭 > New > Other... > Spring Bean Configuration File
생성 완료!
예제로 이해하기
1. 날짜 2020.12.25 출력하기 (java.util.Date 클래스 이용)
New > Class > BeanTest.java 생성하고 다음과 같이 작성
BeanTest.java
package com.test01;
import java.util.Date;
public class BeanTest {
public BeanTest() {
System.out.println("기본 생성자!");
}
public BeanTest(Date date) {
System.out.println("파라미터 1개 생성자(Date date) : " + date);
}
}
applicationContext.xml
<constructor-arg> : 생성자를 통해 주입 ( 여기서는 public BeanTest(Date date) )
<constructor-arg ref=""> : bean 객체를 주입 ( 여기서는 date라는 이름을 가진 bean )
<?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">
<!-- 1. 2020.12.25 -->
<bean id="date" class="java.util.Date">
<constructor-arg name="year" value="120"></constructor-arg>
<constructor-arg name="month" value="11"></constructor-arg>
<constructor-arg name="date" value="25"></constructor-arg>
</bean>
<bean id="beanTest" class="com.test01.BeanTest">
<!-- 1. public BeanTest(Date date) 호출 - 2020.12.25 -->
<constructor-arg name="date" ref="date"></constructor-arg>
</bean>
</beans>
New > Class > MTest.java 생성하고 다음과 같이 작성
MTest.java
beanTest라는 이름을 가진 bean 객체를 호출
package com.test01;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MTest {
public static void main(String[] args) {
ApplicationContext factory = new ClassPathXmlApplicationContext("com/test01/applicationContext.xml");
BeanTest beans = (BeanTest) factory.getBean("beanTest");
}
}
2. setter 이용하기 ( 클래스 )
New > Class > MyClass.java 생성하고 다음과 같이 작성
MyClass.java
package com.test01;
public class MyClass {
public MyClass() {
System.out.println("MyClass()");
}
}
BeanTest.java에 MyClass 필드 작성, setter 작성
BeanTest.java
package com.test01;
import java.util.Date;
public class BeanTest {
private MyClass myClass;
public BeanTest() {
System.out.println("기본 생성자!");
}
public BeanTest(Date date) {
System.out.println("파라미터 1개 생성자(Date date) : " + date);
System.out.println();
}
public void setMyClass(MyClass myClass) {
this.myClass = myClass;
System.out.println("setMyClass(MyClass myClass) 호출");
}
}
applicationContext.xml 에 myClass bean 객체 생성, property 추가
applicationContext.xml
<property> : setter를 호출 ( 여기서는 setMyClass(MyClass myClass) )
<!-- 2. -->
<bean id="myClass" class="com.test01.MyClass"></bean>
<bean id="beanTest" class="com.test01.BeanTest">
<!-- 1. public BeanTest(Date date) 호출 - 2020.12.25 -->
<constructor-arg name="date" ref="date"></constructor-arg>
<!-- 2. setMyClass(MyClass myClass) 호출 -->
<property name="myClass" ref="myClass"></property>
</bean>
MTest.java
package com.test01;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MTest {
public static void main(String[] args) {
ApplicationContext factory = new ClassPathXmlApplicationContext("com/test01/applicationContext.xml");
MyClass my = (MyClass) factory.getBean("myClass");
BeanTest beans = (BeanTest) factory.getBean("beanTest");
}
}
applicationContext.xml에서 lazy-init 사용하기
lazy-init : bean 생성을 늦출 수 있습니다.
lazy-init="false" : 시작 시 생성합니다. ( default )
lazy-init="true" : 특정 bean에 대한 요청이 있을 시 생성합니다. ( 여기서는 myClass bean 객체를 호출했을 때 생성됨 )
<!-- 2. -->
<!-- lazy-init : 호출을 늦춤 -->
<bean id="myClass" class="com.test01.MyClass" lazy-init="true"></bean>
<bean id="beanTest" class="com.test01.BeanTest">
<!-- 1. public BeanTest(Date date) 호출 - 2020.12.25 -->
<constructor-arg name="date" ref="date"></constructor-arg>
<!-- 2. setMyClass(MyClass myClass) 호출 -->
<property name="myClass" ref="myClass"></property>
</bean>
3. setter 이용하기 ( 숫자 )
BeanTest.java
package com.test01;
public class BeanTest {
public void setNumber(int i) {
System.out.println();
System.out.println("setNumber(int i) 호출 : " + i);
}
}
applicationContext.xml
<bean id="beanTest" class="com.test01.BeanTest">
<!-- 4. setNumber(int i) 호출 -->
<property name="number">
<value type="short">27</value> <!-- 묵시적 형 변환 -->
</property>
</bean>
setter의 파라미터로 들어가는 값의 타입을 short라고 지정해주어도 묵시적 형 변환이 일어나 27로 잘 나옵니다.
4. setter 이용하기 ( Array )
BeanTest.java
package com.test01;
public class BeanTest {
public BeanTest() {
System.out.println("기본 생성자!");
}
public void setArray(String[] arr) {
System.out.println();
System.out.println("setArray(String[] arr) 호출 : ");
for (String str : arr) {
System.out.println(str);
}
}
}
applicationContext.xml
<bean id="beanTest" class="com.test01.BeanTest">
<!-- 5. setArray(String[] arr) 호출 -->
<property name="array">
<array>
<value>홍길동</value>
<value>이순신</value>
<value>백승아</value>
</array>
</property>
</bean>
5. setter 이용하기 ( Collection - List , Set , Map )
BeanTest.java
package com.test01;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class BeanTest {
public BeanTest() {
System.out.println("기본 생성자!");
}
public void setList(List<String> list) {
System.out.println();
System.out.println("setList(List<String> list) 호출");
for (String str : list) {
System.out.println(str);
}
}
public void setMySet(Set<String> set) {
System.out.println();
System.out.println("setMySet(Set<String> set) 호출");
for (String str : set) {
System.out.println(str);
}
}
public void setMap(Map<String, String> map) {
System.out.println();
System.out.println("setMap(Map<String, String> map) 호출");
Collection<String> keys = map.keySet();
Collection<String> values = map.values();
for (String key : keys) {
System.out.println(key + " : " + map.get(key));
}
}
}
applicationContext.xml
<bean id="beanTest" class="com.test01.BeanTest">
<!-- 6. setList(List<String> list) 호출 -->
<property name="list">
<list>
<value>봄</value>
<value>여름</value>
<value>가을</value>
<value>겨울</value>
</list>
</property>
<!-- 7. setMySet(Set<String> set) 호출 -->
<property name="mySet">
<!-- 중복을 허용하지 않음 -->
<set>
<value>1</value>
<value>1</value>
<value>2</value>
<value>3</value>
<value>3</value>
</set>
</property>
<!-- 8. setMap(Map<String, String> map) 호출 - 1:봄, 2:여름, 3:가을, 4:겨울 -->
<property name="map">
<map>
<entry key="1">
<value>봄</value>
</entry>
<entry>
<key><value>2</value></key>
<value>여름</value>
</entry>
<entry key="3" value="가을"></entry>
<entry key="4" value="겨울" />
</map>
</property>
</bean>
6. setter 이용하기
New > Class > Score.java 생성하고 다음과 같이 작성
package com.test01;
public class Score {
private String name;
private int kor;
private int eng;
private int math;
public Score() {
}
public Score(String name, int kor, int eng, int math) {
this.name = name;
this.kor = kor;
this.eng = eng;
this.math = math;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getKor() {
return kor;
}
public void setKor(int kor) {
this.kor = kor;
}
public int getEng() {
return eng;
}
public void setEng(int eng) {
this.eng = eng;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
@Override
public String toString() {
return "Score [name=" + name + ", kor=" + kor + ", eng=" + eng + ", math=" + math + "]";
}
}
BeanTest.java
package com.test01;
import java.util.List;
public class BeanTest {
public void setScore(List<Score> score) {
System.out.println("setScore(List<Score> score) 호출");
for (Score sc : score) {
System.out.println(sc);
}
}
}
applicationContext.xml
<bean id="beanTest" class="com.test01.BeanTest">
<!-- 9. setScore(List<Score> score) 호출 -->
<property name="score">
<list>
<bean class="com.test01.Score">
<property name="name" value="홍길동"></property>
<property name="kor" value="90"></property>
<property name="eng" value="88"></property>
<property name="math" value="59"></property>
</bean>
<!-- 특정 bean을 가지고 옴 -->
<ref bean="lee" />
</list>
</property>
</bean>
<!-- 9. -->
<bean id="lee" class="com.test01.Score">
<constructor-arg value="이순신"></constructor-arg>
<constructor-arg value="66"></constructor-arg>
<constructor-arg value="99"></constructor-arg>
<constructor-arg value="80"></constructor-arg>
</bean>
'Spring > 이론' 카테고리의 다른 글
[Spring] 스프링 AOP ( Aspect Oriented Programming ) (0) | 2020.03.24 |
---|---|
[Spring] 어노테이션 ( Annotation ) (0) | 2020.03.24 |
[Spring] 스프링 시작하기 ( Eclipse Marketplace 설치 ) (0) | 2020.03.18 |
[Spring] 스프링 프레임워크 ( Spring Framework ) 의 개념과 특징 (0) | 2020.03.16 |