2011년 11월 12일 토요일

SpringFramework Construct Injection

applicationContext.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">

    <bean id="user" class="com.domain.User" >
       <!-- Contruct  Injection  -->
       <constructor-arg index="0" value="홍길동"></constructor-arg>
       <constructor-arg index="1" value="10"></constructor-arg>
       <constructor-arg index="2" value="한국"></constructor-arg>
    </bean>
</beans>

applicationContext-2.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">

   <bean id="user2" class="com.domain.User" >
      <!-- Contruct  Injection  -->
      <constructor-arg value="name"></constructor-arg>
      <constructor-arg value="12"></constructor-arg>
      <constructor-arg value="Korea"></constructor-arg>    
   
   </bean>

</beans>

User.java   method
package com.domain;

public class User {
   
    private String name;
    private int    age;
    private String country;
   
   // Construct Injection 에 사용되는 Method
    public User(String name,int age,String country){
        this.name = name;
        this.age  = age;
        this.country = country;
    }
   
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
   

    public String toString (){
        return "Name : " + name + " : " + age + " : " + country;
    }
   
}

UserApp.java

package com.service;

import javax.annotation.Resources;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

import com.domain.User;

public class UserApp {

   
    public static void main (String[] args){
       //  static Log
        final Log logger = LogFactory.getLog(UserApp.class);
       
        logger.info("Logger Start ");
       
   //   ------------------ 방법 1 ----------------------------------//
   //  FileSystemResource ("") 경로에 주의                                          //
   //     Resource res = new FileSystemResource("src/beans.xml");
   //     XmlBeanFactory factory = new XmlBeanFactory(res);


   //  ------------------- 방법 2 ----------------------------------//   
   //  클래스 패스의 경로에 위치                                                         //
   //   ClassPathResource resource = new ClassPathResource ("beans.xml");
   //   XmlBeanFactory factory = new XmlBeanFactory(resource);
   
   // ----------------------- 방법 3 --------------------------------//   
        ClassPathXmlApplicationContext appContext =
                  new ClassPathXmlApplicationContext (new String[] {
                      "applicationContext.xml" , "applicationContext-2.xml"     
                  });
        BeanFactory factory = (BeanFactory) appContext;
       
        User user = (User) factory.getBean("user");
        User user2 = (User) factory.getBean("user2");

   // ------------------------- 방법 4 -------------------------------//
   //     ApplicationContext context = new ClassPathXmlApplicationContext ("beans.xml");
   //     User user = (User) context.getBean("user");

        logger.info ("User Class method Start" + user.toString());
        System.out.println(user.toString());
        System.out.println(user2.toString());
       
       
       
    }
}


댓글 없음:

댓글 쓰기