DI 프로젝트 복사해서 AutoWrie 프로젝트 만듬
pom.xml
위에 artifactID 이름 변경해줘야함
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>AutoWire</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<!--자바 버전 -->
<java-version>11</java-version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<!-- 스프링 버전 -->
<org.springframework-version>5.3.20</org.springframework-version>
<!--로그버전 -->
<org.slf4j-version>1.7.32</org.slf4j-version>
<ch.qos.logback-version>1.2.11</ch.qos.logback-version>
</properties>
<!-- 프로젝트에서 사용할 라이브러리 정보 -->
<dependencies>
<!--spring-context 스프링코어 라이브러리 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- slf4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<!-- logback -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${ch.qos.logback-version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
업데이트
TestBean1
package com.demo.beans;
public class TestBean1 {
private DataBean1 data1;
private DataBean1 data2;
public DataBean1 getData1() {
return data1;
}
public void setData1(DataBean1 data1) {
this.data1 = data1;
}
public DataBean1 getData2() {
return data2;
}
public void setData2(DataBean1 data2) {
this.data2 = data2;
}
}
TestBean2
package com.demo.beans;
public class TestBean2 {
private DataBean2 data1;
private DataBean2 data2;
public DataBean2 getData1() {
return data1;
}
public void setData1(DataBean2 data1) {
this.data1 = data1;
}
public DataBean2 getData2() {
return data2;
}
public void setData2(DataBean2 data2) {
this.data2 = data2;
}
}
POJO
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">
<!--testbean1을 가지고 만듬, getset메소드가 있어서 set메소드를 가지고 data1을 set메소드로 data_bean1객체 입력 -->
<!--테스트빈1객체 생성하고, data1에 아래의 객체를 주입(set메소드주입) -->
<bean id="obj1" class="com.demo.beans.TestBean1">
<!-- property 는 set메소드를 사용해서 데이터 입력함 -->
<property name="data1" ref="data_bean1"/>
<property name="data2" ref="data_bean1"/>
</bean>
<bean id="data_bean1" class="com.demo.beans.DataBean1" scope="prototype"/>
</beans>
1. 생성자 주입
2. set메소드 주입
main
package com.demo.main;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.demo.beans.TestBean1;
public class MainClass {
public static void main(String[] args) {
// beans.xml 파일을 로딩한다
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("com/demo/config/beans.xml");
TestBean1 obj1 = ctx.getBean("obj1", TestBean1.class);
System.out.printf("obj1.data1 : %s\n", obj1.getData1());
System.out.printf("obj1.data2 : %s\n", obj1.getData2());
System.out.println("--------------------------");
ctx.close();
}
}
결과확인
09:22:26.515 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@32d2fa64
09:22:26.665 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loaded 2 bean definitions from class path resource [com/demo/config/beans.xml]
09:22:26.686 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'obj1'
obj1.data1 : com.demo.beans.DataBean1@1c7696c6
obj1.data2 : com.demo.beans.DataBean1@60099951
--------------------------
09:22:26.744 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Closing org.springframework.context.support.ClassPathXmlApplicationContext@32d2fa64, started on Thu Oct 19 09:22:26 KST 2023
prototype이라서 새로운 객체 생성함
기본 객체 사용할 때는 singletone임. (scanner,connection 등)
prototype은 객체를 한 번 쓰고 버릴 때 사용함 .
이름을 통한 주입
autowire = "byName"
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">
<!--testbean1을 가지고 만듬, getset메소드가 있어서 set메소드를 가지고 data1을 set메소드로 data_bean1객체 입력 -->
<!--테스트빈1객체 생성하고, data1에 아래의 객체를 주입(set메소드주입) -->
<bean id="obj1" class="com.demo.beans.TestBean1">
<!-- property 는 set메소드를 사용해서 데이터 입력함 -->
<property name="data1" ref="data_bean1"/>
<property name="data2" ref="data_bean1"/>
</bean>
<!--포로트타입으로 새로운 객체 생성 (기본은 싱글톤) -->
<bean id="data_bean1" class="com.demo.beans.DataBean1" scope="prototype"/>
<!-- 오토와이어 : 이름 (이름이 같은 객체를 주입한다.) -->
<bean id="obj2" class="com.demo.beans.TestBean1" autowire="byName"/>
<bean id="data1" class="com.demo.beans.DataBean1" />
<bean id="data2" class="com.demo.beans.DataBean1" />
</beans>
main에서 출력하기
09:29:21.838 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@32d2fa64
09:29:21.982 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loaded 5 bean definitions from class path resource [com/demo/config/beans.xml]
09:29:22.003 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'obj1'
09:29:22.047 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'obj2'
09:29:22.049 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'data1'
09:29:22.049 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'data2'
obj1.data1 : com.demo.beans.DataBean1@2ddc9a9f
obj1.data2 : com.demo.beans.DataBean1@298a5e20
--------------------------
obj2.data1 : com.demo.beans.DataBean1@2a7f1f10
obj2.data2 : com.demo.beans.DataBean1@46cdf8bd
--------------------------
09:29:22.064 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Closing org.springframework.context.support.ClassPathXmlApplicationContext@32d2fa64, started on Thu Oct 19 09:29:21 KST 2023
byName 으로 이름이 같고, 객체 타입도 동일해야함
타입을 통한 주입
autowire = "byType"
beans.xml
<!-- 오토와이어 : 타입 (타입이 같은 객체를 주입한다. 이때 같은 타입의 객체는 하나이어야 함) -->
<bean id="obj3" class="com.demo.beans.TestBean2" autowire="byType" />
<!-- 유일한 데이터빈2 객체 등록(이름 없음) -->
<bean class="com.demo.beans.DataBean2" />
main 출력
package com.demo.main;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.demo.beans.TestBean1;
import com.demo.beans.TestBean2;
public class MainClass {
public static void main(String[] args) {
// beans.xml 파일을 로딩한다
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("com/demo/config/beans.xml");
TestBean1 obj1 = ctx.getBean("obj1", TestBean1.class);
System.out.printf("obj1.data1 : %s\n", obj1.getData1());
System.out.printf("obj1.data2 : %s\n", obj1.getData2());
System.out.println("--------------------------");
TestBean1 obj2 = ctx.getBean("obj2", TestBean1.class);
System.out.printf("obj2.data1 : %s\n", obj2.getData1());
System.out.printf("obj2.data2 : %s\n", obj2.getData2());
System.out.println("--------------------------");
TestBean2 obj3 = ctx.getBean("obj3", TestBean2.class);
System.out.printf("obj3.data1 : %s\n", obj3.getData1());
System.out.printf("obj3.data2 : %s\n", obj3.getData2());
System.out.println("--------------------------");
ctx.close();
}
}
출력 결과
obj3.data1 : com.demo.beans.DataBean2@78e4deb0
obj3.data2 : com.demo.beans.DataBean2@78e4deb0
--------------------------
09:34:05.728 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Closing org.springframework.context.support.ClassPathXmlApplicationContext@32d2fa64, started on Thu Oct 19 09:34:05 KST 2023
data1 , data2 주소가 같음.
beans.xml에서
singletone이라서 하나만 생성돼서 그럼
만약 두 개가 있으면?
<!-- 오토와이어 : 타입 (타입이 같은 객체를 주입한다. 이때 같은 타입의 객체는 하나이어야 함) -->
<bean id="obj3" class="com.demo.beans.TestBean2" autowire="byType" />
<!-- 유일한 데이터빈2 객체 등록(이름 없음) -->
<bean class="com.demo.beans.DataBean2" />
<bean class="com.demo.beans.DataBean2" />
에러남
autoWire는 기본으로 byType인데 만약 객체가 여러 개 이면 byName으로 해야함
생성자를 통한 주입
beans.xml
<!-- 생성자 주입 -->
<bean id="obj4" class="com.demo.beans.TestBean2" autowire="constructor" />
TestBean2에 생성자 추가
package com.demo.beans;
public class TestBean2 {
private DataBean2 data1;
private DataBean2 data2;
public TestBean2() {
// 기본생성자 obj3때문에 오류남
}
public TestBean2(DataBean2 data1, DataBean2 data2) {
super();
this.data1 = data1;
this.data2 = data2;
}
public DataBean2 getData1() {
return data1;
}
public void setData1(DataBean2 data1) {
this.data1 = data1;
}
public DataBean2 getData2() {
return data2;
}
public void setData2(DataBean2 data2) {
this.data2 = data2;
}
}
obj3는 Type으로 주입하기 때문에 기본 생성자를 만듬
constructor는 생성자로 주입하기 때문에 따로 만듬
main에서 확인
TestBean2 obj4 = ctx.getBean("obj4", TestBean2.class);
System.out.printf("obj4.data1 : %s\n", obj4.getData1());
System.out.printf("obj4.data2 : %s\n", obj4.getData2());
System.out.println("--------------------------");
obj4.data1 : com.demo.beans.DataBean2@1cf6d1be
obj4.data2 : com.demo.beans.DataBean2@1cf6d1be
--------------------------
09:42:33.738 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Closing org.springframework.context.support.ClassPathXmlApplicationContext@32d2fa64, started on Thu Oct 19 09:42:33 KST 2023
autowire은 type , name, constructor 으로 주입할 수 있다.
constructor(생성자) type으로 자동 주입된다.
'BACKEND > Spring' 카테고리의 다른 글
LegacyProject 시작하기 , DB 연결을 위한 라이브러리 추가 (1) | 2023.10.19 |
---|---|
레거시 프로그램 설치 및 기본 설정 (0) | 2023.10.19 |
DI 생성자 주입 (1) | 2023.10.18 |
bean 객체의 생명 주기 - 소멸 (0) | 2023.10.18 |
bean 객체 생성하기 (0) | 2023.10.18 |