스프링에서는 new로 객체를 생성하지 않는다 라고 하는데..


기본적인 사용법 예제>

- 해당 라이브러리를 web-inf/lib에 복사
- 자바 콘솔 모드로 .

 

파일목록>

(1) MyPerson.java
(2) TestMain.java
(3) test01.xml

 

 

(1) MyPerson.java
: 멤버변수 선언, getter, setter

package test.di1;

public class MyPerson {
    private String name;
    private int age;
    private String phone;
   
    public MyPerson(){}
   
    public MyPerson(String name, int age, String phone) {
        super();
        this.name = name;
        this.age = age;
        this.phone = phone;
    }
    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 getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
}

 

(2) TestMain.java

package test.di1;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestMain {
    public static void main(String[] args) {
        MyPerson per = new MyPerson();
        //per객체에 값을 저장하고 출력해 보세요.
        per.setName("KIM");
        per.setAge(10);
        per.setPhone("010-000-9999");
       
        System.out.println("이름:" + per.getName());
        System.out.println("나이:" + per.getAge());
        System.out.println("전화번호:" + per.getPhone());
    }
}

 

new로 생성해서 getter 메소드 호출하여 출력한다.

 

이것을 spring으로 바꾸면


 

package test.di1;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestMain {
    public static void main(String[] args) {
//        MyPerson per = new MyPerson();
//        //per객체에 값을 저장하고 출력해 보세요.
//        per.setName("hong");
//        per.setAge(10);
//        per.setPhone("010");
//       
//        System.out.println("이름:" + per.getName());
//        System.out.println("나이:" + per.getAge());
//        System.out.println("전화번호:" + per.getPhone());
       
        String res="test/di1/test01.xml";
        //classpath의 위치에 있는 test01.xml을 읽어와 객체를 생성해줌.
       ApplicationContext context = new ClassPathXmlApplicationContext(res);
        
        //생성된 객체중 id가 person인 객체를 꺼내온다.
        MyPerson person = (MyPerson)context.getBean("person");
        System.out.println("이름:" + person.getName());
        System.out.println("나이:" + person.getAge());
        System.out.println("전화번호:" + person.getPhone());
    }
}

; context.getBean는 object타입으로 반환하기 때문에 형변환해줘야 한다.
; new를 하지 않았는데 person변수 사용하고 있다.
  person.getName()...




(3) test01.xml

; xml을 생성해야 한다. spring 체험을 위해.

<?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-3.1.xsd">
   
    <!--  빈 설정파일(스프링이 생성할 자바객체에 대한 주문서) -->
   
    <!-- MyPerson타입의 객체 person을 만들어라. -->
    <!-- default 생성자를 호출함. -->
    <bean id="person" class="test.di1.MyPerson">
        <!-- 멤버변수name에 '홍길동'을 넣어라 -->
        <!-- setter메소드 -->
        <property name="name" value="KIM"/>
        <!-- 멤버변수 age에 10을 넣어라. -->
        <property name="age" value="10"/>
        <property name="phone" value="010-000-1111"/>
    </bean>
</beans>



결과는 동일하다. beans 라는 것을 이용해서 ..

xml 은 STS(spring tool suite)에서는 해당위치에서 마우스 우측 / new/other/






spring bean configuration file선택.
default 로 선택하고 나중에
해당 namespace를 변경할수 있다.

+ Recent posts