ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 9장 DI 자동주입
    스프링 프레임워크 기초 공부 2020. 4. 27. 01:03
    package kr.co.softcampus.beans;
    
    public class DataBean1 {
    	
    }
    

    DataBean1클래스

     

    package kr.co.softcampus.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;
    	}
    }
    

    TestBean1클래스

     

    <?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='kr.co.softcampus.beans.TestBean1'>
    		<property name='data1' ref='data_bean1'/>
    		<property name='data2' ref='data_bean1'/>
    	</bean>
    	
    	<bean id='data_bean1' class='kr.co.softcampus.beans.DataBean1' scope='prototype'/><!-- 주입될때마다 새로운객체생성 -->

    위 xml은 직접 주입해주는 방법이다

    1. TestBean1객체를 생성하여 obj1라는 id만든다

    2. 주입할때 마다 새로운 객체를 만드는 scope='prototype을 사용하여 DataBean1객체를 생성한다

    3.setter 주입을 사용하여 2번에서 만든 DataBean1객체를 data1에 data2에 각각 주입시킨다.

     

    <!-- 자동주입방법 -->
    	
    	
    	<bean id='obj2' class='kr.co.softcampus.beans.TestBean1' autowire="byName"/>
    	<bean id='data1' class='kr.co.softcampus.beans.DataBean1'/>
    	<bean id='data2' class='kr.co.softcampus.beans.DataBean1'/>

     

    위 xml은 자동으로 주입해주는 방법이다

    autowire="byname"을 사용하면 빈 객체의 프로터피와 정의된 빈의 이름이 같은 것을 찾아 자동으로 주입해준다

    자동주입은 클래스 객체타입만 가능하다 정수형이나 문자열타입은 직접주입해줘야함

     

    package kr.co.softcampus.main;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import kr.co.softcampus.beans.TestBean1;
    import kr.co.softcampus.beans.TestBean2;
    
    public class MainClass {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("kr/co/softcampus/config/beans.xml");
    		
    		TestBean1 obj1 = ctx.getBean("obj1",TestBean1.class);
    		System.out.println(obj1.getData1());
    		System.out.println(obj1.getData2());
    		
    		System.out.println("------------------------");
    		TestBean1 obj2 = ctx.getBean("obj2",TestBean1.class);
    		System.out.println(obj2.getData1());
    		System.out.println(obj2.getData2());
    		
    		ctx.close();//ioc컨테이너 해제 객체들 소멸됨
    	}
    
    }
    

     

    직접 주입과 자동 주입을 main클래스에 객체화한 후 결과를 보겠다.

     

    결과창

    kr.co.softcampus.beans.DataBean1@15b204a1
    kr.co.softcampus.beans.DataBean1@77167fb7
    ------------------------
    kr.co.softcampus.beans.DataBean1@1fe20588
    kr.co.softcampus.beans.DataBean1@6ce139a4

     

    직접주입과 자동주입 두 방법다 객체가 잘 주입된것을 볼 수 있다.

     

     

    타입을 통한 주입

     

    TestBean2 클래스

    package kr.co.softcampus.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;
    	}
    	
    	
    	
    }

     

     

    xml

        <bean id='obj3' class="kr.co.softcampus.beans.TestBean2" autowire="byType"/>
    	<bean class='kr.co.softcampus.beans.DataBean2'/> 

    TestBean2객체에서 프로퍼티 타입이 DataBean2가 있다면 자동으로 주입시켜줍니다

    (이때 DataBean2객체가 한개이상 선언되어있다면 오류가 발생합니다)

     

    main 클래스

    package kr.co.softcampus.main;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import kr.co.softcampus.beans.TestBean1;
    import kr.co.softcampus.beans.TestBean2;
    
    public class MainClass {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("kr/co/softcampus/config/beans.xml");
    
    		TestBean2 obj3 = ctx.getBean("obj3", TestBean2.class);
    		System.out.println(obj3.getData1());
    		System.out.println(obj3.getData2());
    
    		ctx.close();// ioc컨테이너 해제 객체들 소멸됨
    	}
    
    }

     

    출력창

    kr.co.softcampus.beans.DataBean2@4f933fd1
    kr.co.softcampus.beans.DataBean2@4f933fd1

     

    생성자를 통한 주입

     

     

    TestBean3

    package kr.co.softcampus.beans;
    
    public class TestBean3 {
    	private int data1;
    	private String data2;
    	private DataBean2 data3;
    	private DataBean2 data4;
    	
    	public TestBean3(DataBean2 data3, DataBean2 data4) {
    		this.data3 = data3;
    		this.data4 = data4;
    	}
    	
    	public TestBean3(int data1, String data2, DataBean2 data3, DataBean2 data4) {
    		this.data1 = data1;
    		this.data2 = data2;
    		this.data3 = data3;
    		this.data4 = data4;
    	}
    	
    	public int getData1() {
    		return data1;
    	}
    	public void setData1(int data1) {
    		this.data1 = data1;
    	}
    	public String getData2() {
    		return data2;
    	}
    	public void setData2(String data2) {
    		this.data2 = data2;
    	}
    	public DataBean2 getData3() {
    		return data3;
    	}
    	public void setData3(DataBean2 data3) {
    		this.data3 = data3;
    	}
    	public DataBean2 getData4() {
    		return data4;
    	}
    	public void setData4(DataBean2 data4) {
    		this.data4 = data4;
    	}
    	
    	
    }

     

    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='data_bean2' class='kr.co.softcampus.beans.DataBean2'/>
    	<!-- <bean class='kr.co.softcampus.beans.DataBean2'/>-->
    	
    	<bean id='obj5' class='kr.co.softcampus.beans.TestBean3' autowire="constructor"/>
    	
    
    </beans>
    
    
    

    생성자의 매개변수타입과 xml에서 생성된 객체의 타입이 같다면 자동으로 주입시켜줍니다.

     

     

    main class

    package kr.co.softcampus.main;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import kr.co.softcampus.beans.TestBean1;
    import kr.co.softcampus.beans.TestBean2;
    import kr.co.softcampus.beans.TestBean3;
    
    public class MainClass {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("kr/co/softcampus/config/beans.xml");
    		
    
    		
    		TestBean3 obj5 = ctx.getBean("obj5", TestBean3.class);
    		System.out.printf("obj5.data1 : %d\n", obj5.getData1());
    		System.out.printf("obj5.data2 : %s\n", obj5.getData2());
    		System.out.printf("obj5.data3 : %s\n", obj5.getData3());
    		System.out.printf("obj5.data4 : %s\n", obj5.getData4());
    		
    		
    		ctx.close();
    	}
    
    }
    
    
    

     

    출력창

    obj5.data1 : 0
    obj5.data2 : null
    obj5.data3 : kr.co.softcampus.beans.DataBean2@28feb3fa
    obj5.data4 : kr.co.softcampus.beans.DataBean2@28feb3fa

     

    xml 파일 namespace에 기본 자동주입설정 -> bean객체를 생성할때 autowire가 선언되있지않은 객체는 모두 이 기본값이 적용됨

     

    <?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"
    	default-autowire="byName">

     

    기본값적용 해제하는방법

    <bean id='obj9' class='kr.co.softcampus.beans.TestBean1' autowire="no"/>

     

Designed by Tistory.