본문 바로가기

Java/Spring

스프링 프레임웍 기본 예제


MessageBean Interface (MesageBean.java)
package sample;

public interface MessageBean {
	void sayHello(String name);
}
MessageBeanEn Class (MessageBeanEn.java)
package sample;

public class MessageBeanEn implements MessageBean{
	@Override
	public void sayHello(String name){
		System.out.println("Hello," + name + "!");
	}
}
MessageBeanKo Class (MessageBeanKo.java)
package sample;

public class MessageBeanKo implements MessageBean{
	@Override
	public void sayHello(String name){
		System.out.println("안녕하세요!" + name + "씨");
	}
}
HelloApp Class (HelloApp.java - Application Class)
package sample;

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 HelloApp {
	public static void main(String[] args) {
		Resource resource = new ClassPathResource("applicationContext.xml");
		BeanFactory factory = new XmlBeanFactory(resource);
		MessageBean bean = (MessageBean)factory.getBean("messageBean");
		bean.sayHello("Spring");
	}
}
applicationContext.xml (Spring Bean Configuration File = Spring Bean Definition File)