https://blog.naver.com/drv982/222900664820
Dependency Injection 의존성 주입
A객체를 만들려고 하는데 B객체가 필요할경우
스프링 프레임워크에서는 B객체를 자동으로 만들어줌
이런거를 의존성 주입이라고 함.
DI 생성
pom.xml 복사 붙여넣기
maven 업데이트
패키지도 3개 다 복사함
안의 내용 다 지우기
TestBean
package com.demo.beans;
public class TestBean {
private int data1;
private double data2;
private String data3;
// 기본 생성자
public TestBean() {
System.out.println("TestBean의 기본 생성자");
this.data1 = 0;
this.data2 = 0.0;
this.data3 = null;
}
public TestBean(int data1) {
System.out.println("TestBean의 생성자 : int 변수 1");
this.data1 = data1;
this.data2 = 0.0;
this.data3 = null;
}
public TestBean(double data2) {
System.out.println("TestBean의 생성자 : double 변수 1");
this.data1 = 0;
this.data2 = data2;
this.data3 = null;
}
public TestBean(String data3) {
System.out.println("TestBean의 생성자 : String 변수 1");
this.data1 = 0;
this.data2 = 0.0;
this.data3 = data3;
}
public TestBean(int data1, double data2, String data3) {
System.out.println("TestBean의 생성자 : 변수 3개");
this.data1 = data1;
this.data2 = data2;
this.data3 = data3;
}
// 출력 메소드
public void printData() {
System.out.printf("data1 : %d\n", data1);
System.out.printf("data2 : %f\n", data2);
System.out.printf("data3 : %s\n", data3);
}
}
모든 생성자 사용
beans.xml
<?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='obj1' class='com.demo.beans.TestBean' lazy-init='true' />
<!--시작 태그와 종료 태그 사이에 생성자 -->
<!-- 생성자 입력변수로 정수(int) 100이 입력됨. -->
<bean id='obj2' class='com.demo.beans.TestBean' lazy-init='true'>
<constructor-arg value='100' type='int' />
</bean>
<!-- 생성자 입력변수로 실수(double) 11.11이 입력됨. -->
<bean id='obj3' class='com.demo.beans.TestBean' lazy-init='true'>
<constructor-arg value='11.11' type='double' />
</bean>
<!-- 생성자 입력변수로 문자열(String)이 입력됨. -->
<bean id='obj4' class='com.demo.beans.TestBean' lazy-init='true'>
<constructor-arg value='문자열' />
</bean>
<!-- 생성자 입력변수로 정수,실수,문자열 모두 입력됨. -->
<bean id='obj5' class='com.demo.beans.TestBean' lazy-init='true'>
<constructor-arg value='200' type='int' />
<constructor-arg value='22.22' type='double' />
<constructor-arg value='안녕하세요' type='java.lang.String' />
</bean>
</beans>
main
package com.demo.main;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.demo.beans.TestBean;
public class MainClass {
public static void main(String[] args) {
// beans.xml 파일을 로딩한다
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("com/demo/config/beans.xml");
TestBean obj1 = ctx.getBean("obj1", TestBean.class);
obj1.printData();
System.out.println("--------------------------");
TestBean obj2 = ctx.getBean("obj2", TestBean.class);
obj2.printData();
System.out.println("--------------------------");
TestBean obj3 = ctx.getBean("obj3", TestBean.class);
obj3.printData();
System.out.println("--------------------------");
TestBean obj4 = ctx.getBean("obj4", TestBean.class);
obj4.printData();
System.out.println("--------------------------");
TestBean obj5 = ctx.getBean("obj5", TestBean.class);
obj5.printData();
System.out.println("--------------------------");
}
}
출력
15:18:39.645 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@32d2fa64
15:18:39.791 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loaded 5 bean definitions from class path resource [com/demo/config/beans.xml]
15:18:39.830 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'obj1'
TestBean의 기본 생성자
data1 : 0
data2 : 0.000000
data3 : null
--------------------------
15:18:39.843 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'obj2'
TestBean의 생성자 : int 변수 1
data1 : 100
data2 : 0.000000
data3 : null
--------------------------
15:18:39.862 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'obj3'
TestBean의 생성자 : double 변수 1
data1 : 0
data2 : 11.110000
data3 : null
--------------------------
15:18:39.864 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'obj4'
TestBean의 생성자 : String 변수 1
data1 : 0
data2 : 0.000000
data3 : 문자열
--------------------------
15:18:39.866 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'obj5'
TestBean의 생성자 : 변수 3개
data1 : 200
data2 : 22.220000
data3 : 안녕하세요
--------------------------
beans.xml 추가
obj8~9
<!-- 테스트빈2는 객체를 입력받음. -->
<bean id='obj8' class='com.demo.beans.TestBean2' lazy-init='true'>
<constructor-arg>
<bean class='com.demo.beans.DataBean' />
</constructor-arg>
<constructor-arg>
<bean class='com.demo.beans.DataBean' />
</constructor-arg>
</bean>
<bean id='data_bean' class='com.demo.beans.DataBean'
scope='prototype' />
<!-- 위의 data_bean 객체를 자동 주입받는다. 객체 두 번 생성 -->
<bean id='obj9' class='com.demo.beans.TestBean2' lazy-init='true'>
<constructor-arg ref='data_bean' />
<constructor-arg ref='data_bean' />
</bean>
DataBean
package com.demo.beans;
public class DataBean {
}
TestBean2
package com.demo.beans;
public class TestBean2 {
//DataBean 객체를 변수를 선언함
private DataBean data1;
private DataBean data2;
//생성 시 DataBean 객체를 입력 받는다.
public TestBean2(DataBean data1, DataBean data2) {
super();
this.data1 = data1;
this.data2 = data2;
}
// 출력 메소드
public void printData() {
System.out.printf("data1 : %s\n", data1);
System.out.printf("data2 : %s\n", data2);
}
}
빈 설정으로 값을 주입하는 방법이 생성자 변수로
obj8-9 객체를 주입받음
생성자 주입으로 객체를 주입받음
main에서 테스트
TestBean2 obj8 = ctx.getBean("obj8", TestBean2.class);
obj8.printData();
System.out.println("-------------------------------");
TestBean2 obj9 = ctx.getBean("obj9", TestBean2.class);
obj9.printData();
ctx.close();
--------------------------
15:40:10.031 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'obj8'
data1 : com.demo.beans.DataBean@1ed6388a
data2 : com.demo.beans.DataBean@5a45133e
-------------------------------
15:40:10.033 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'obj9'
data1 : com.demo.beans.DataBean@60bd273d
data2 : com.demo.beans.DataBean@121314f7
15:40:10.038 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Closing org.springframework.context.support.ClassPathXmlApplicationContext@32d2fa64, started on Wed Oct 18 15:40:09 KST 2023
'BACKEND > Spring' 카테고리의 다른 글
레거시 프로그램 설치 및 기본 설정 (0) | 2023.10.19 |
---|---|
DI - 자동 주입 (1) | 2023.10.19 |
bean 객체의 생명 주기 - 소멸 (0) | 2023.10.18 |
bean 객체 생성하기 (0) | 2023.10.18 |
IOC 컨테이너? (0) | 2023.10.18 |