2014년 5월 20일 화요일

[오라클자바커뮤니티 스프링교육강좌]Spring IoC, DI어노테이션기반 예제(Spring Framework Annotation, @Autowired, @Component, @Service)

[오라클자바커뮤니티 스프링교육강좌]Spring IoC, DI어노테이션기반 예제(Spring Framework Annotation, @Autowired, @Component, @Service)


오라클자바커뮤니티몰 어노테이션 예제

이전에 작성한 오라클자바커뮤니티몰 예제를 Annotation 기반으로 변경하자. BeanFactory가 아닌 ApplicationContext를 사용해야 한다. BeanFactory의 경우 애노테이션을 지원하지 않고 자바빈 역시 필요한 시점에 메모리에 로딩하므로 적절하지 않다.

1. Money.java
package edu.biz.ioc4;

import org.springframework.stereotype.Component;

//Money클래스를 money라는 이름으로 로딩
//단순한 DTO 같은 컴포넌트임을 표시
@Component("money")
public class Money {
private int amount;
public Money() {
}
public Money(int amt) {
this.amount = amt;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
}

2. Car.java

package edu.biz.ioc4;

import org.springframework.stereotype.Component;

//Car클래스를 car라는 이름으로 로딩
//단순한 DTO 같은 컴포넌트임을 표시
@Component("car")
public class Car {
       
        private String name;
       
        public Car() {}

        public Car(String name) {
               this.name = name;
        }

        public String getName() {
               return name;
        }

        public void setName(String name) {
               this.name = name;
        }
}


3. CarMaker.java

package edu.biz.ioc4;

public interface CarMaker {
        public Car sell(Money money);
}




4. DaewooMaker.java

package edu.biz.ioc4;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

//서비스계층의 클래스임을 위미
@Service("daewoo")
public class DaewooMaker implements CarMaker {

        //TYPE Car인 객체를 찾아 자동으로 주입   
        @Autowired
        private Car car;
       
        public Car sell(Money money) {
               //차를 팔기전에 할 일들
               System.out.println("I sold a Tosca.");
               car.setName("Tosca");
               return car;
        }
}



5. HyundaiMaker.java


package edu.biz.ioc4;

import org.springframework.stereotype.Service;
//서비스계층의 클래스임을 위미
@Service("hyundai")
public class HyundaiMaker implements CarMaker{
       //TYPE Car인 객체를 찾아 자동으로 주입   
        @Autowired
        private Car car;

        public Car sell(Money money) {
               System.out.println("I sold a Sonata.");
               car.setName("Sonata");
               return car;
        }
}


6. OrderManager.java


package edu.biz.ioc4;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
//서비스계층의 클래스임을 위미
@Service("oraderManager")
public class OrderManager {
        private String name;
       
        //TYPE CarMaker인 객체를 찾아 자동으로 주입
        //현재 hyundai, daewoo 두개가 있으므로 @Qualifier로 하나를 명시
        @Autowired
        @Qualifier("daewoo")
        private CarMaker carMaker;
       
        @Autowired
        private Money money;

        public OrderManager() {
        }
        public void order() {
               money.setAmount(1000);
               Car car = this.carMaker.sell(money);
        }   
}



7. src/main/resources/ioc4.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-3.2.xsd">
        <context:annotation-config/>
        <context:component-scan base-package="edu.biz.ioc4"/>
</beans>




8. OrderManagerApp.java


package edu.biz.ioc4;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
                                 
public class OrderManagerApp {
        public static void main(String[] args) {            
               ApplicationContext factory =  
               new ClassPathXmlApplicationContext("classpath:ioc4.xml");
               OrderManager manager = (OrderManager) factory.getBean("orderManager");
               manager.order();
        }
}

댓글 없음:

댓글 쓰기