App.java 파일--------------------------------------------
package test1;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class App {
public static void main(String[] args) throws Exception {
BeanFactory factory = new XmlBeanFactory(new FileSystemResource(
"src//test1//hello.xml"));
GreetingService a = (GreetingService) factory
.getBean("greetingService");
a.sayGreeting();
a.sayHello();
GreetingService b = (GreetingService) factory.getBean("helloService");
b.sayGreeting();
b.sayHello();
}
}
GreetingService .java 파일-----------------------------------
package test1;
public interface GreetingService {
public void sayGreeting();
public void sayHello();
}
GreetingServiceImpl .java 파일------------------------------
package test1;
public class GreetingServiceImpl implements GreetingService {
private String greeting;
private String hello;
public GreetingServiceImpl() {
}
public void setHello(String hello) {
this.hello = hello;
}
public GreetingServiceImpl(String greeting) {
this.greeting = greeting;
}
public void setGreeting(String greeting) {
this.greeting = greeting;
}
public void sayHello() {
System.out.println(hello);
}
public void sayGreeting() {
System.out.println(greeting);
}
}
hello.xml 파일-----------------------------------
<?xml version="1.0" ENCODING="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="greetingService" class="test1.GreetingServiceImpl">
<property name="greeting">
<value>Not Real!</value>
</property>
<property name="hello">
<value>So..So</value>
</property>
</bean>
<bean id="helloService" class="test1.GreetingServiceImpl">
<property name="hello">
<value>hello</value>
</property>
</bean>
</beans>
결과물..
2008. 5. 12 오후 5:39:32 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
정보: Loading XML bean definitions from file [D:\Expert\Spring\src\test1\hello.xml]
Not Real!
So..So
null
hello
-------------
주의할 점은 xml파일의 경로를
D:\Expert\Spring\src\test1\hello.xml ( 절대경로 사용방법)
src\\test1\\hello.xml (자신의 폴더가 프로젝트폴더인곳에서의
경로인 상대경로 사용방법) 를 사용하는 방법 두가지가 있다.
같은 package 내 라고 하여 단순히 hello.xml 을 사용할경우
파일을 못찾는경우가 생기며
src 의 하위폴더라고하여 test1 이라는 패키지부터 사용할 경우
또한 파일을 못찾는다...