ドラムの上手な例---spring


HelloBean.java
package onlyfun.caterpillar;

public class HelloBean
{
private String helloWord = "Hello!World!";

public void setHelloWord(String helloWord)
{
this.helloWord = helloWord;
}

public String getHelloWord()
{
return helloWord;
}
}
<?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="helloBean" class="onlyfun.caterpillar.HelloBean">
      <property name="helloWord"><value>Hello!Justin!</value></property>
</bean>
</beans>

package onlyfun.caterpillar;
import org.springframework.core.io.ClassPathResource;

import java.io.*;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
//XMLBeanFactory       BeanFactory    
public class SpringTest 
{
public static void main(String[] args) throws IOException
{

ClassPathResource resource = new ClassPathResource("bean.xml");
BeanFactory factory = new XmlBeanFactory(resource);

HelloBean hello = (HelloBean) factory.getBean("helloBean"); 
System.out.println(hello.getHelloWord());
}
}