一个简单的spring

类别:Java 点击:0 评论:0 推荐:

bean.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="helloBean" class="danke.HelloBean">
        <property name="helloWord"><value>Hello!man!</value></property>
    </bean>
</beans>

HelloBean.java

package danke;
public class HelloBean {
    private String helloWord = "Hello!World!";
    public void setHelloWord(String helloWord) {
        this.helloWord = helloWord;
    }
    public String getHelloWord() {

        return helloWord;
    }
}

SpringTest.java

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

       
        new SpringTest();
    }
    public SpringTest()
    {
          String abc=new String("abc");
     System.out.println(abc);
      
       
        try
        {
         //BeanFactory factory = new XmlBeanFactory((Resource)is);
         InputStream is = getClass().getResourceAsStream("bean.xml");
         System.out.println(abc);
   XmlBeanFactory factory = new XmlBeanFactory(is);

         HelloBean hello = (HelloBean) factory.getBean("helloBean");
         System.out.println(hello.getHelloWord());
        }
        catch(Exception e)
        {
         e.printStackTrace();
        }
         System.out.println(abc);
    }
}

本文地址:http://com.8s8s.com/it/it9996.htm