2016년 9월 28일 수요일

02day Spring

1. Bean 옵션에 따라서 인스턴스를 생성시키는 방법이 달라짐

폴더구성

HelloSpring.java
package spring;

public class HelloSpring {
 
 public HelloSpring() {
  // TODO Auto-generated constructor stub
  System.out.println("생성자 출력");
 }
 
 public void sayHello(String name){
  System.out.println(name + "님 안녕하세요");
 }
}

context.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-4.1.xsd">

    <bean id="helloBean1" class="spring.HelloSpring" scope="prototype"/><!-- prototype쓰면 겟빈을 하는 순간 인스턴스화 각각이루어짐  -->
    <bean id="helloBean2" class="spring.HelloSpring" scope="singleton"/>
    
</beans>

HelloSpringMain.java
package spring;

import org.springframework.context.support.GenericXmlApplicationContext;

public class HelloSpringMain {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:spring/context.xml");
  
  //prototype쓰면 겟빈을 하는 순간 인스턴스화 각각이루어짐
  HelloSpring helloSpring1 = (HelloSpring)ctx.getBean("helloBean1", HelloSpring.class);
  HelloSpring helloSpring2 = (HelloSpring)ctx.getBean("helloBean1", HelloSpring.class);

  //객체 주소 출력
  System.out.println(helloSpring1);
  System.out.println(helloSpring2);
  
  
  ctx.close();
 }

}

생성자가 bean의 singleton때문에 generic때 한번, prototype 인스턴스 호출 때문에 2번 이루어짐

HelloSpringMain2.java
package spring;

import org.springframework.context.support.GenericXmlApplicationContext;

public class HelloSpringMain {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:spring/context.xml");
  
  //singleton쓰면 겟빈을 하는 순간 인스턴스 한번 생성
  HelloSpring helloSpring1 = (HelloSpring)ctx.getBean("helloBean2", HelloSpring.class);
  HelloSpring helloSpring2 = (HelloSpring)ctx.getBean("helloBean2", HelloSpring.class);
  
  //객체 주소 출력
  System.out.println(helloSpring1);
  System.out.println(helloSpring2);
  
  
  ctx.close();
 }

}

생성자가 bean의 singleton때문에 generic때 한번 이루어짐

2. 생성자 주입- 사용자 정의 생성자를 부르는 법

폴더구성

WriteAction.java
package spring3;

public class WriteAction {
 private String name;
 private String age;
 
 public WriteAction() {
  // 겟빈이 디폴트로 부르는 기본 생성자
  System.out.println("디폴트 생성자 호출");
 }

 public WriteAction(String name) {
  this.name = name;
  System.out.println("유저 생성자 호출 1");
 }
 
 public WriteAction(String name, String age) {
  this.name = name;
  System.out.println("유저 생성자 호출 2");
 }
 
}

context.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-4.1.xsd">

    <bean id="action1" class="spring3.WriteAction" scope="prototype"/><!-- 디폴트 생성자 부름 -->
    <bean id="action2" class="spring3.WriteAction" scope="prototype"><!-- 사용자 정의 생성자 1 부름 -->
        <constructor-arg value="홍길동" />
    </bean>
    <bean id="action3" class="spring3.WriteAction" scope="prototype"><!-- 사용자 정의 생성자 2 부름 -->
        <constructor-arg value="홍길동" />
        <constructor-arg value="30" />
    </bean>
</beans>

ApplicationMain.java
package spring3;

import org.springframework.context.support.GenericXmlApplicationContext;

public class ApplicationMain {

 public static void main(String[] args) {
  // singleton 이면 Generic 할때 모든 생성자를 다부름
  GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:spring3/context.xml");
  
  //WriteAction action1 = (WriteAction)ctx.getBean("action1");
  //WriteAction action2 = (WriteAction)ctx.getBean("action2");
  WriteAction action3 = (WriteAction)ctx.getBean("action3");
  
  ctx.close();
 }

}



**bean이 singleton인 경우 -한개의 생성자만 사용하더라도 모두다 호출됨
context.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-4.1.xsd">

    <bean id="action1" class="spring3.WriteAction"/><!-- 디폴트 생성자 부름 -->
    <bean id="action2" class="spring3.WriteAction"><!-- 사용자 정의 생성자 1 부름 -->
        <constructor-arg value="홍길동" />
    </bean>
    <bean id="action3" class="spring3.WriteAction"><!-- 사용자 정의 생성자 2 부름 -->
        <constructor-arg value="홍길동" />
        <constructor-arg value="30" />
    </bean>

</beans>

ApplicationMain.java
package spring3;

import org.springframework.context.support.GenericXmlApplicationContext;

public class ApplicationMain {

 public static void main(String[] args) {
  // singleton 이면 Generic 할때 모든 생성자를 다부름
  GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:spring3/context.xml");
  
  //WriteAction action1 = (WriteAction)ctx.getBean("action1");
  //WriteAction action2 = (WriteAction)ctx.getBean("action2");
  WriteAction action3 = (WriteAction)ctx.getBean("action3");
  
  ctx.close();
 }

}




자바프로젝트와 비교

WriteAction.java

public class WriteAction {
 private String name;
 
 public WriteAction() {
  // 겟빈이 디폴트로 부르는 기본 생성자
  System.out.println("디폴트 생성자");
 }

 public WriteAction(String name) {
  this.name = name;
  System.out.println("유저 생성자 호출");
 }
 
}

ApplicationMain.java

public class ApplicationMain {
 public static void main(String[] args) {
  //기본생성자
  WriteAction action1 = new WriteAction();
  //사용자정의 생성자
  WriteAction action2 = new WriteAction("홍길동");
 }
}


3. 생성자 매개변수에 TO클래스가 들어가는 경우

폴더구성

BoardTO.java
package spring3;

public class BoardTO {
 public BoardTO() {
  System.out.println("BoardTO 생성자 호출");
 }
}

WriteAction.java
package spring3;

public class WriteAction {
 private String name;
 private String age;
 
 public WriteAction() {
  // 겟빈이 디폴트로 부르는 기본 생성자
  System.out.println("디폴트 생성자 호출");
 }

 public WriteAction(String name) {
  this.name = name;
  System.out.println("유저 생성자 호출 1");
 }
 
 public WriteAction(String name, String age) {
  this.name = name;
  System.out.println("유저 생성자 호출 2");
 }
 public WriteAction(BoardTO to){
  System.out.println("유저 생성자 호출 3");
 }
}

context.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-4.1.xsd">

    <bean id="action1" class="spring3.WriteAction" scope="prototype"/><!-- 디폴트 생성자 부름 -->
    <bean id="action2" class="spring3.WriteAction" scope="prototype"><!-- 사용자 정의 생성자 1 부름 -->
        <constructor-arg value="홍길동" />
    </bean>
    <bean id="action3" class="spring3.WriteAction" scope="prototype"><!-- 사용자 정의 생성자 2 부름 -->
        <constructor-arg value="홍길동" />
        <constructor-arg value="30" />
    </bean>
    <bean id="to" class="spring3.BoardTO" scope="prototype" /><!-- 사용할 TO클래스를 먼저 정의함 -->
    <bean id="action4" class="spring3.WriteAction" scope="prototype"><!-- 사용자 정의 생성자 3 부름 -->
        <constructor-arg ref="to" />
    </bean>
</beans>

ApplicationMain.java
package spring3;

import org.springframework.context.support.GenericXmlApplicationContext;

public class ApplicationMain {

 public static void main(String[] args) {
  // singleton 이면 Generic 할때 모든 생성자를 다부름
  GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:spring3/context.xml");
  
  //WriteAction action1 = (WriteAction)ctx.getBean("action1");
  //WriteAction action2 = (WriteAction)ctx.getBean("action2");
  //WriteAction action3 = (WriteAction)ctx.getBean("action3");
  WriteAction action4 = (WriteAction)ctx.getBean("action4");
  
  ctx.close();
 }

}


4.boardTO 객체 사용

폴더구성


BoardTO.java
package spring4;

public class BoardTO {
 private int seq;
 private String subject;
 private String content;
 
 public int getSeq() {
  return seq;
 }
 public void setSeq(int seq) {
  this.seq = seq;
 }
 public String getSubject() {
  return subject;
 }
 public void setSubject(String subject) {
  this.subject = subject;
 }
 public String getContent() {
  return content;
 }
 public void setContent(String content) {
  this.content = content;
 }
}

context.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-4.1.xsd">

    <bean id="to" class="spring4.BoardTO">
        <property name="seq" value="1" />
        <property name="subject" value="제목" />
        <property name="content" value="내용" />
    </bean>
</beans>

ApplicationMain.java
package spring4;

import org.springframework.context.support.GenericXmlApplicationContext;

public class ApplicationMain {

 public static void main(String[] args) {
  GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:spring4/context.xml");
  
  BoardTO to = (BoardTO)ctx.getBean("to");
  System.out.println(to.getSeq());
  System.out.println(to.getSubject());
  System.out.println(to.getContent());
  
  to.setSeq(2); //BoardTO를 바꾸면 context.xml에도 적용이 됨
  System.out.println(to.getSeq());
  to = (BoardTO)ctx.getBean("to");
  System.out.println(to.getSeq());
  
  ctx.close();
  System.out.println(to.getSeq());//객체가 소멸되었지만 일시적으로 찍힐 수 있음
 }

}



자바 프로젝트와 비교
폴더구성

BoardTO.java
package spring2;

public class BoardTO {
 private int seq;
 private String subject;
 private String content;
 
 public int getSeq() {
  return seq;
 }
 public void setSeq(int seq) {
  this.seq = seq;
 }
 public String getSubject() {
  return subject;
 }
 public void setSubject(String subject) {
  this.subject = subject;
 }
 public String getContent() {
  return content;
 }
 public void setContent(String content) {
  this.content = content;
 }
}

ApplicationMain.java
package spring2;

public class ApplicationMain {
 public static void main(String[] args) {
  BoardTO to = new BoardTO();
  
  to.setSeq(1);
  to.setSubject("제목");
  to.setContent("내용");
  
  System.out.println(to.getSeq());
  System.out.println(to.getSubject());
  System.out.println(to.getContent());
 }
}


5.ListTO 객체의 사용

폴더구성


BoardTO.java
package spring5;

public class BoardTO {
 private int seq;
 private String subject;
 private String content;
 
 public int getSeq() {
  return seq;
 }
 public void setSeq(int seq) {
  this.seq = seq;
 }
 public String getSubject() {
  return subject;
 }
 public void setSubject(String subject) {
  this.subject = subject;
 }
 public String getContent() {
  return content;
 }
 public void setContent(String content) {
  this.content = content;
 }
}

BoardListTO.java
package spring5;

import java.util.ArrayList;

public class BoardListTO {
    private ArrayList<String> users;
    private ArrayList<BoardTO> boardLists;
    
    public ArrayList<String> getUsers() {
        return users;
    }
    public void setUsers(ArrayList<String> users) {
        this.users = users;
    }
    public ArrayList<BoardTO> getBoardLists() {
        return boardLists;
    }
    public void setBoardLists(ArrayList<BoardTO> boardLists) {
        this.boardLists = boardLists;
    }
}

context.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-4.1.xsd">
    <bean id="to1" class="spring5.BoardTO" scope="prototype"><!-- boardListTO에 넣을 boardTO 생성 -->
        <property name="seq" value="1" />
        <property name="subject" value="제목1" />
        <property name="content" value="내용1" />
    </bean>
    <bean id="to2" class="spring5.BoardTO" scope="prototype">
        <property name="seq" value="2" />
        <property name="subject" value="제목2" />
        <property name="content" value="내용2" />
    </bean>
    
    <bean id="listTO" class="spring5.BoardListTO" scope="prototype"><!-- boardListTO 생성 -->
        <property name="users"><!-- 직접 boardListTO에 입력-->
            <list>
                <value>홍길동</value>
                <value>박문수</value>
            </list>
        </property>
        <property name="boardLists"><!-- 위에서 정의한 boardTO를 boardListTO에 입력-->
            <list>
                <ref bean="to1"/>
                <ref bean="to2"/>
            </list>
        </property>
    </bean>
</beans>

ApplicationMain.java
package spring5;

import org.springframework.context.support.GenericXmlApplicationContext;

public class ApplicationMain {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:spring5/context.xml");
  
  BoardListTO listTO = (BoardListTO)ctx.getBean("listTO");
  for(String user : listTO.getUsers()){
   System.out.println(user);
  }
  
  for(BoardTO to : listTO.getBoardLists()){
   System.out.println(to.getSeq());
   System.out.println(to.getSubject());
   System.out.println(to.getContent());
  }
  
  ctx.close();
 }

}



자바프로젝트와 비교
폴더구성

BoardTO.java
package spring3;

public class BoardTO {
 private int seq;
 private String subject;
 private String content;
 
 public int getSeq() {
  return seq;
 }
 public void setSeq(int seq) {
  this.seq = seq;
 }
 public String getSubject() {
  return subject;
 }
 public void setSubject(String subject) {
  this.subject = subject;
 }
 public String getContent() {
  return content;
 }
 public void setContent(String content) {
  this.content = content;
 }
}

BoardListTo.java
package spring3;

import java.util.ArrayList;

public class BoardListTo {
    private ArrayList<String> users;
    private ArrayList<BoardTO> boardLists;
    
    public ArrayList<String> getUsers() {
        return users;
    }
    public void setUsers(ArrayList<String> users) {
        this.users = users;
    }
    public ArrayList<BoardTO> getBoardLists() {
        return boardLists;
    }
    public void setBoardLists(ArrayList<BoardTO> boardLists) {
        this.boardLists = boardLists;
    }
}

ApplicationMain.java
package spring3;

import java.util.ArrayList;

public class ApplicationMain {

    public static void main(String[] args) {
        //객체 생성
        ArrayList<String> users = new ArrayList<>();
        users.add("홍길동");
        users.add("박문수");
        
        BoardListTo listTO = new BoardListTo();
        listTO.setUsers(users);
        
        //객체 사용
        for(String user : listTO.getUsers()){
            System.out.println(user);
        }
        
        //객체 생성 (스프링에서 처리하는 부분)
        BoardTO to1 = new BoardTO();
        to1.setSeq(1);
        to1.setSubject("제목1");
        to1.setContent("내용1");
        BoardTO to2 = new BoardTO();
        to2.setSeq(2);
        to2.setSubject("제목2");
        to2.setContent("내용2");
        
        ArrayList<BoardTO> boardLists = new ArrayList<>();
        boardLists.add(to1);
        boardLists.add(to2);
        listTO.setBoardLists(boardLists);
        
        //객체 사용
        for(BoardTO to : listTO.getBoardLists()){
            System.out.println(to.getSeq());
            System.out.println(to.getSubject());
            System.out.println(to.getContent());
        }
        
    }

}


6. 스프링에서 bean을 생성할때 사용되는 메서드와 bean생명주기

폴더구조


Action.java 인터페이스 만듦
package spring;

public interface Action {
 public abstract void sayHello();
}


WriteAction.java 만들때 인터페이스 상속받음
package spring;
//스프링에서 bean만들 때 자동적으로 호출되는 메서드들
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class WriteAction implements Action, BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean {
    
    private String msg;
    private String beanName;
    private BeanFactory beanFactory;
    
    public WriteAction() {
        // TODO Auto-generated constructor stub
        System.out.println("1. 빈의 생성자 실행");
    }
    @Override
    public void destroy() throws Exception {
        // TODO Auto-generated method stub
        System.out.println("9. destroy() 실행");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        // TODO Auto-generated method stub
        System.out.println("6. afterPropertiesSet() 실행");
    }

    @Override
    public void setBeanFactory(BeanFactory arg0) throws BeansException {
        // TODO Auto-generated method stub
        System.out.println("4. setBeanFactory() 실행");
        this.beanFactory = arg0;
        System.out.println("\t ->" +beanFactory);
    }

    @Override
    public void setBeanName(String arg0) {
        // TODO Auto-generated method stub
        System.out.println("3. setBeanName() 실행");
        this.beanName = arg0;
        System.out.println("\t ->" +beanName);
    }

    @Override
    public void sayHello() {
        // TODO Auto-generated method stub
        System.out.println("*. sayHello() 실행");
        System.out.println("\t ->" +beanName);
    }

}


context.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-4.1.xsd">

    <bean id="action" class="spring.WriteAction" />
</beans>

ApplicationMain.java
package spring;

import org.springframework.context.support.GenericXmlApplicationContext;

public class ApplicationMain {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:spring/context.xml");
  
  Action action = (Action)ctx.getBean("action");
  action.sayHello();
  
  ctx.close();
 }

}


7. 스프링에서 bean 초기화 및 삭제 메서드 지정

폴더구조


WriteAction.java
package spring;
//스프링에서 bean만들 때 자동적으로 호출되는 메서드들
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class WriteAction implements Action, BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean {
    
    private String msg;
    private String beanName;
    private BeanFactory beanFactory;
    
    public WriteAction() {
        System.out.println("1. 빈의 생성자 실행");
    }
    @Override
    public void destroy() throws Exception {
        System.out.println("9. destroy() 실행");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("6. afterPropertiesSet() 실행");
    }

    @Override
    public void setBeanFactory(BeanFactory arg0) throws BeansException {
        System.out.println("4. setBeanFactory() 실행");
        this.beanFactory = arg0;
        System.out.println("\t -&gt;" +beanFactory);
    }

    @Override
    public void setBeanName(String arg0) {
        System.out.println("3. setBeanName() 실행");
        this.beanName = arg0;
        System.out.println("\t -&gt;" +beanName);
    }

    @Override
    public void sayHello() {
        System.out.println("*. sayHello() 실행");
        System.out.println("\t -&gt;" +beanName);
    }
    //초기화 때 사용할 메서드 지정
    public void init_method(){
        System.out.println("7. init_method() 실행");
    }
    //Bean삭제 때 사용할 메서드 지정
    public void destroy_method(){
        System.out.println("10. destroy_method() 실행");
    }

}

context.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-4.1.xsd">
    
    <!-- 초기화와 삭제할때 사용할 메서드 지정 -->
    <bean id="action" class="spring.WriteAction" init-method="init_method" destroy-method="destroy_method" />
</beans>

ApplicationMain.java
package spring;

import org.springframework.context.support.GenericXmlApplicationContext;

public class ApplicationMain {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:spring/context.xml");
  
  Action action = (Action)ctx.getBean("action");
  action.sayHello();
  
  ctx.close();
 }

}


8. 스프링에서 bean 멤버변수 세팅 메서드 순서

WriteAction.java
package spring;
//스프링에서 bean만들 때 자동적으로 호출되는 메서드들
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class WriteAction implements Action, BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean {
    
    private String msg;
    private String beanName;
    private BeanFactory beanFactory;
    
    public WriteAction() {
        System.out.println("1. 빈의 생성자 실행");
    }
    //setter
    public void setMsg(String msg){
        this.msg = msg;
        System.out.println("2. setMsg() 실행");
    }
    
    @Override
    public void destroy() throws Exception {
        System.out.println("9. destroy() 실행");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("6. afterPropertiesSet() 실행");
        //setter로 멤버변수 세팅 되어있는지 확인하는 메서드
        if(msg ==null){
            System.out.println("멤버변수 필요함");
        }else{
            System.out.println("멤버변수 있음");
        }
    }

    @Override
    public void setBeanFactory(BeanFactory arg0) throws BeansException {
        System.out.println("4. setBeanFactory() 실행");
        this.beanFactory = arg0;
        System.out.println("\t ->" +beanFactory);
    }

    @Override
    public void setBeanName(String arg0) {
        System.out.println("3. setBeanName() 실행");
        this.beanName = arg0;
        System.out.println("\t ->" +beanName);
    }

    @Override
    public void sayHello() {
        System.out.println("*. sayHello() 실행");
        System.out.println("\t ->" +beanName);
    }

    public void init_method(){
        System.out.println("7. init_method() 실행");
    }
    public void destroy_method(){
        System.out.println("10. destroy_method() 실행");
    }

}

context.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-4.1.xsd">
    
    <bean id="action" class="spring.WriteAction" init-method="init_method" destroy-method="destroy_method" >
        <property name="msg" value="Hello" /><!-- 멤버변수 세팅 -->
    </bean>
</beans>

ApplicationMain.java
package spring;

import org.springframework.context.support.GenericXmlApplicationContext;

public class ApplicationMain {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:spring/context.xml");
  
  Action action = (Action)ctx.getBean("action");
  action.sayHello();
  
  ctx.close();
 }

}


댓글 없음:

댓글 쓰기