English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
여기서, 우리는 첫 번째 spring应用程序을 생성하는 간단한 단계를 배웁니다. 이应用程序을 실행하려면 어떤 IDE도 사용하지 않습니다. 우리는 단순히 명령 프롬프트를 사용합니다. spring应用程序을 생성하는 간단한 단계를 확인해 보겠습니다.
Java 클래스 생성 값을 제공하는 xml 파일 생성 테스트 클래스 생성 spring jar 파일 로드 테스트 클래스 실행
우리는 첫 번째 spring을 만들어 보겠습니다.5단계
이는 이름 속성을 포함하는 간단한 Java bean 클래스입니다.
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()라는 추가 메서드를 포함하고 있으며, 이 메서드는 인사말을 통해 학생 이름을 출력합니다.
만약 myeclipse IDE를 사용한다면, xml 파일을 생성할 필요가 없습니다. myeclipse는 이 작업을 자체적으로 완료할 수 있습니다. 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 클래스 객체에 설정합니다.
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(); } }
자원은 applicationContext.xml 파일의 정보를 나타냅니다. Resource는 인터페이스이고 ClassPathResource 은 Resource 인터페이스 구현 클래스입니다. Resource는 인터페이스이며 BeanFactory 은 Bean을 반환하는 책임을 가집니다. XmlBeanFactory 은 BeanFactory 구현 클래스입니다. BeanFactory 인터페이스에는 많은 메서드가 있습니다. 하나의 메서드는 getBean()이 메서드는 관련 클래스의 객체를 반환합니다.
이 애플리케이션을 실행하기 위해 주로 세 가지 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 파일을 다운로드하세요
core, web, aop, mvc, j와 같은 모든 Spring jar 파일을 다운로드하세요.2ee, remoting, oxm, jdbc, orm 등.
이 예제를 실행하려면 단순히 spring core jar 파일을 로드하면 됩니다.
Test 클래스를 실행하세요. Hello: Vimal Jaiswal로 출력을 받게 됩니다.