[Spring] 스프링 빈 설정파일 ( Spring Bean Configuration File...
[Spring] 스프링 빈 설정파일 ( Spring Bean Configuration File...
1. src 우클릭 > com.test01 패키지 생성
2. com.test01 우클릭 > New > Other... > Spring Bean Configuration File
Spring Bean Configuration File 선택
applicationContext.xml 입력 후 Finish
생성 완료!
예제로 이해하기
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
: 생성자를 통해 주입 ( 여기서는 public BeanTest(Date date) )
: bean 객체를 주입 ( 여기서는 date라는 이름을 가진 bean )
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"); } }
Ctrl + F11로 실행 > Console 결과
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
: setter를 호출 ( 여기서는 setMyClass(MyClass myClass) )
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 객체를 호출했을 때 생성됨 )
실행 결과
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
27
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
홍길동 이순신 백승아
실행 결과
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 list) { System.out.println(); System.out.println("setList(List list) 호출"); for (String str : list) { System.out.println(str); } } public void setMySet(Set set) { System.out.println(); System.out.println("setMySet(Set set) 호출"); for (String str : set) { System.out.println(str); } } public void setMap(Map map) { System.out.println(); System.out.println("setMap(Map map) 호출"); Collection keys = map.keySet(); Collection values = map.values(); for (String key : keys) { System.out.println(key + " : " + map.get(key)); } } }
applicationContext.xml
봄 여름 가을 겨울 1 1 2 3 3 봄 2 여름
실행 결과
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) { System.out.println("setScore(List score) 호출"); for (Score sc : score) { System.out.println(sc); } } }
applicationContext.xml
실행 결과
from http://printf100.tistory.com/37 by ccl(A) rewrite - 2020-03-18 22:20:28
댓글
댓글 쓰기