2014년 12월 11일 목요일

Spring IoC의 DI 세터 주입(Setter Injection) 예제 - XML기반[JAVA/JSP/Spring/JDBC/자바프레임워크/스프링기초실무학원/전액무료교육]


Spring IoC DI중 하나인 Setter 주입 예제를 만들어 보자.

1.     [/src/main/resources/xmlBeanFactory.xml]


<?xml version="1.0" encoding="UTF-8"?>
      
       <bean id="car" class="onj.spring.di.Car"/>

       <bean id="audi" class="onj.spring.di.Audi">
             <property name="car"><ref bean="car"/></property>   
       </bean>     
</beans>


2.     CarMaker.java


package onj.spring.di;

public interface CarMaker {
              public Car sell();
}


3.     Car.java

package onj.spring.di;

public class Car {
       private String name;
         public Car() {
         }
         public Car(String name) {
            this.name = name;
         }
         public String getName() {
            return this.name;
         }
         public void setName(String name) {
            this.name  = name;
         }
}



4.     Audi.java(Audi setCar 메소드로 Car 인스턴스가 주입된다)

package onj.spring.di;


public class Audi implements CarMaker {
       private Car car;
      
       public Audi() {}
      
      
       public void setCar(Car car) {
             this.car = car;
       }
      

       public Car sell() {
             this.car.setName("Audi A6");
             return car;
       }
}



5.     SetterInjectionExam.java


package onj.spring.di;

import org.springframework.context.support.GenericXmlApplicationContext;

public class SetterInjectionExam {
   public static void main(String[] args) {
          GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
          ctx.load("classpath:xmlBeanFactory.xml");
          ctx.refresh();
         
          CarMaker carMaker = ctx.getBean("audi", Audi.class);

          Car c = carMaker.sell();
          System.out.println("I sold a car... " + c.getName());
   }
}
  





 [100%환급,실무전문]SQL/빅데이터/자바/스프링/웹퍼블리싱/안드…오라클자바…12-272810
 [채용확정무료교육]오라클자바개발잘하는신입뽑기2개월과정,교육…오라클자바…12-112043
53 [평일100%환급7건]웹퍼블리싱,자바&JSP,안드로이드,C#닷넷,SQL기… 오라클자바…03-151867
52 [주말]C#,ASP.NET마스터 오라클자바…01-312024
51 [기업100%환급,평일주간]SQL기초에서스키마오브젝트,PLSQL,힌트… 오라클자바…01-312988
50 [주말주간]자바&웹,jQUERY,스프링프레임워크,마이바티스 오라클자바…01-311560
49 [평일주간/야간,주말주간/야간]Spring,MyBatis,Hibernate개발자… 오라클자바…01-191882
48 [평일주간/야간,주말주간/야간]안드로이드개발자과정(Adnroid 교… 오라클자바…01-111756
47 [평일야간,주말주간야간]JAVA,Network&JSP&Spring,MyBatis,Hiber… 오라클자바…01-032332
46 [100%환급,실무전문]SQL/빅데이터/자바/스프링/웹퍼블리싱/안드… 오라클자바…12-272810
45 [주말야간]개발자를위한PLSQL,SQL튜닝,힌트(토/일) 오라클자바…12-191969
44 [평일주간/야간,주말주간/야간]웹퍼블리싱 마스터(HTML5,CSS3,jQ… 오라클자바…12-141949
43 [채용확정무료교육]오라클자바개발잘하는신입뽑기2개월과정,교육… 오라클자바…12-112043
42 [주말야간]JAVA,JSP,Spring,PLSQL,힌트,웹퍼블리싱,안드로이드,… 오라클자바…12-091589
41 [평일야간,주말야간]닷넷(C#,Network,ADO.NET,ASP.NET)마스터 오라클자바…12-011813
40 [기업100%환급]오라클&자바웹스프링신입과정3주(SQL,JAVA,JSP,Se… 오라클자바…12-012039
39 [평일야간,주말]SQL기초에서실무까지(SQL기초,PLSQL,힌트,튜닝) 오라클자바…12-011465

댓글 없음:

댓글 쓰기