2015년 10월 12일 월요일

main메소드에서 getBean()을 생성자주입으로[Spring DI,Constructor Injection]

main메소드에서 getBean()을 생성자주입으로 대체한 예제[Spring DI,Constructor Injection]


0. Money.java

package edu.biz.ioc4;

import org.springframework.stereotype.Component;

@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;
}
}


1. Car.java

package edu.biz.ioc4;

import org.springframework.stereotype.Component;

@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;
}
}



2. CarMaker.java

package edu.biz.ioc4;

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


3. 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 {

@Autowired
private Car car;
public Car sell(Money money) {
//차를 팔기전에 할 일들
System.out.println("I sold a Tosca.");
car.setName("Tosca");
return car;
}
}



4. HyundaiMaker.java

package edu.biz.ioc4;

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

@Service("hyundai")
public class HyundaiMaker implements CarMaker{
@Autowired
private Car car;
public Car sell(Money money) {
//차를 팔기전에 할 일들
System.out.println("I sold a Sonata.");
car.setName("Sonata");
return car;
}
}

5. 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;
@Autowired
@Qualifier("daewoo")
private CarMaker carMaker;
@Autowired
private Money money;

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



6. OrderManagerapp.java

package edu.biz.ioc4;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

@Service
public class OrderManagerApp {
static OrderManager orderManager;
@Autowired
public OrderManagerApp(OrderManager orderManager) {
OrderManagerApp.orderManager = orderManager;
}
public static void main(String[] args) {
ApplicationContext factory =   
new ClassPathXmlApplicationContext("classpath:ioc4.xml");
//OrderManager manager = (OrderManager) factory.getBean("orderManager");
orderManager.order();
}
}


8. src/main/resource/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>



[결과]

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/java2/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Documents%20and%20Settings/Administrator/.m2/repository/ch/qos/logback/logback-classic/1.0.13/logback-classic-1.0.13.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
INFO : org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@104a311: startup date [Sat Feb 07 19:58:12 KST 2015]; root of context hierarchy
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [ioc4.xml]
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@bdb19: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,car,daewoo,hyundai,money,oraderManager,orderManagerApp,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
I sold a Tosca.


댓글 없음:

댓글 쓰기