English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

spring 애플리케이션 생성

이곳에서 우리는 eclipse IDE를 사용하여 Spring 프레임워크의 간단한 애플리케이션을 생성할 것입니다. Eclipse IDE에서 Spring 애플리케이션을 생성하는 간단한 단계를 확인해 보겠습니다.

Java 프로젝트 생성 Spring jar 파일 추가 클래스 생성 값을 제공하는 xml 파일 생성 테스트 클래스 생성


Eclipse IDE에서 Spring 애플리케이션을 생성하는 단계

다음 단계를 통해 첫 번째 Spring 애플리케이션을 생성하는 방법을 확인해 보겠습니다.5단계: eclipse IDE.

1Java 프로젝트 생성

이동 파일메뉴- 새로운- 프로젝트- Java 프로젝트프로젝트 이름을 입력하세요. 예를 들어 firstspring- 완료현재 Java 프로젝트가 생성되었습니다.

2Spring jar 파일 추가

이 애플리케이션을 실행하려면 주로 세 개의 jar 파일이 필요합니다.

org.springframework.core-3.0.1.RELEASE-A com.springsource.org.apache.commons.logging-1.1.1 org.springframework.beans-3.0.1.RELEASE-A

미래 사용을 위해 Spring 핵심 애플리케이션에 필요한 jar 파일을 다운로드할 수 있습니다.

Spring의 핵심 jar 파일 다운로드

Spring의 jar 파일을 모두 다운로드하세요. aop, mvc, j2ee, remoting, oxm 등.

이 예제를 실행하려면 단순히 Spring 핵심 jar 파일을 로드하면 됩니다.

Eclipse IDE에서 jar 파일을 로드하려면 프로젝트를 오른쪽 클릭하세요- 빌드 경로- 외부 아카이브 파일 추가- 모든 필요한 파일 jar 파일 선택- 완료..

3Java 클래스를 생성하려면

이 경우 우리는 name 속성을 가진 Student 클래스를 생성하고 있습니다. 학생의 이름은 xml 파일에서 제공됩니다. 이는 단순한 예제이며 실제 Spring 사용이 아닙니다. 실제 사용법은 "의존성 주입" 장에서 볼 수 있습니다. Java 클래스를 생성하려면 src를 오른쪽 클릭하세요 - 새로운- 클래스- 클래스 이름을 입력하세요. 예를 들어, 학생- 완료다음 코드를 작성하세요:

package com.w3codebox;
public class Student {
private String name;
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public void displayInfo(){
    System.out.println("Hello: ");+name);
}
}

이것은 간단한 bean 클래스입니다. 속성 이름을 포함하고 있으며 이를 getter와 setter 메서드로 정의합니다. 이 클래스는 displayInfo()라는 추가 메서드를 포함하고 있으며, 이 메서드는 학생 이름을 인쇄하는 환영 메시지를 출력합니다.

4xml 파일을 생성하세요

xml 파일을 생성하려면 src를 클릭하세요-새로운-file-파일 이름을 지정하세요. 예를 들어, applicationContext.xml-완료. 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"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="studentbean" class="com.w3codebox.Student">
<property name="name" value="Vimal Jaiswal"></property>
</bean>
</beans>

bean 요소는 주어진 클래스에 bean을 정의하는 데 사용됩니다. bean의 property 자식 요소는 name라는 Student 클래스의 속성을 지정합니다. 속성 요소에 지정된 값은 IOC 컨테이너가 Student 클래스 객체에 설정합니다.

5테스트 클래스를 생성하세요

Java 클래스를 생성하세요. 예를 들어, 테스트입니다. 여기서는 BeanFactory의 getBean() 메서드를 사용하여 IOC 컨테이너에서 Student 클래스의 객체를 가져옵니다. 테스트 클래스의 코드를 보겠습니다.

package com.w3codebox;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Test {
public static void main(String[] args) {
    Resource resource = new ClassPathResource("applicationContext.xml");
    BeanFactory factory = new XmlBeanFactory(resource);
    
    Student student = (Student)factory.getBean("studentbean");
    student.displayInfo();
}
}

이제 이 표본을 실행하세요. Hello: Vimal Jaiswal이 출력됩니다.