3-4 Spring DI 활용하기 - 실습

서현우·2022년 5월 17일
0

스프링의정석

목록 보기
35/85

config1.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">
    <!-- scope="prototype"은 매번 새로운 객체를 만듬 -->
    <bean id="car" class="com.fastcampus.ch3.Car">
        <!-- constructor-arg태그는  setter를 사용 -->
        <!-- String, 기본형일 때 value 사용 -->
        <constructor-arg name="color" value="red"/>
        <constructor-arg name="oil" value="100"/>
        <!-- 참조형은 ref 사용 -->
        <constructor-arg name="engine" ref="engine"/>
        <!-- 배열일 때 -->
        <constructor-arg name="doors">
            <array value-type="com.fastcampus.ch3.Door">
                <ref bean="door"/>
                <ref bean="door"/>
            </array>
        </constructor-arg>
    </bean>
    <bean id="engine" class="com.fastcampus.ch3.Engine"/>
    <bean id="door" class="com.fastcampus.ch3.Door"/>
</beans>

config.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
        <context:component-scan base-package="com.fastcampus.ch3">
            <context:exclude-filter type="regex" expression="com.fastcampus.ch3.diCopy*.*"/>
        </context:component-scan>
        <context:annotation-config/>

</beans>

SpringDiTest.java

package com.fastcampus.ch3;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;

//@Component("engine")
class Engine{} //<bean id="engine" class="com.fastcampus.ch3.Engine"/>
@Component class SuperEngine extends Engine {}
@Component class TurboEngine extends Engine {}


@Component
class Car{
    @Value("red") String color;
    @Value("100") int oil;

//    @Autowired //byType
//    @Qualifier("superEngine") //타입으로 먼저 검색 후 n개면 이름으로 검색
    @Resource(name="superEngine") //byName
    Engine engine; //byType - 타입으로 먼저 검색, 여러개면 이름으로 검색. - engine, superEngine, turboEngine
    @Autowired Door[] doors;

    public Car() {} //기본 생성자를 꼭 만들어주자
    public Car(String color, int oil, Engine engine, Door[] doors) {
        this.color = color;
        this.oil = oil;
        this.engine = engine;
        this.doors = doors;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public void setOil(int oil) {
        this.oil = oil;
    }

    public void setEngine(Engine engine) {
        this.engine = engine;
    }

    public void setDoors(Door[] doors) {
        this.doors = doors;
    }

    @Override
    public String toString() {
        return "Car{" +
                "color='" + color + '\'' +
                ", oil=" + oil +
                ", engine=" + engine +
                ", doors=" + Arrays.toString(doors) +
                '}';
    }
}

@Component
class Door{}

public class SpringDiTest {
    public static void main(String[] args) {
        ApplicationContext ac = new GenericXmlApplicationContext("config.xml");
//        Car car = (Car)ac.getBean("car"); //byName. 아래와 같은 문장
        Car car = ac.getBean("car", Car.class);
//        Car car2 = (Car)ac.getBean(Car.class); //byType


//        Engine engine = (Engine)ac.getBean("engine"); //byName
//        Engine engine = (Engine)ac.getBean(Engine.class); //byType - 같은 타입이 3개라서 에러
//        Door door = (Door)ac.getBean("door");

//        car.setColor("red");
//        car.setOil(100);
//        car.setEngine(engine);
//        car.setDoors(new Door[]{ac.getBean("door", Door.class), (Door)ac.getBean("door")});
        System.out.println("car = " + car);


    }
}
profile
안녕하세요!!

0개의 댓글