`
xiangzhengyan
  • 浏览: 124812 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

第一个Spring程序

阅读更多
bean
 
  1. package test;  
  2.   
  3. public class HelloBean {  
  4.     private String helloWord;  
  5.     public void setHelloWord(String helloWord) {  
  6.           this.helloWord = helloWord;  
  7.     }  
  8.     public String getHelloWord() {  
  9.           return helloWord;  
  10.     }  
  11. }  
bean-config.xml
 
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.   xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.   http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">  
  6.      <bean id="helloBean"  
  7.               class="test.HelloBean">  
  8.          <property name="helloWord" value="哈哈哈"><!---->property>  
  9.      <!---->bean>  
  10. <!---->beans>  
测试类
 
  1. package test;  
  2.   
  3. import org.springframework.core.io.ClassPathResource;  
  4. import org.springframework.core.io.Resource;  
  5. import org.springframework.beans.factory.BeanFactory;  
  6. import org.springframework.beans.factory.xml.XmlBeanFactory;  
  7.   
  8. public class MainTest {  
  9.     public static void main(String[] args) {  
  10.         Resource rs = new ClassPathResource("beans-config.xml");  
  11.         BeanFactory factory = new XmlBeanFactory(rs);  
  12.         HelloBean hello = (HelloBean) factory.getBean("helloBean");  
  13.         System.out.println(hello.getHelloWord());  
  14.     }  
  15. }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics