spring IoC编程实例

发布时间:2020-04-09 19:03:27 作者:linzheng
阅读:404
开发者专用服务器限时活动,0元免费领! 查看>>

配置文件

/SpringHelloWorld/src/applicationContext.xml

  1. 代码  
  2.  
  3. <?xml version="1.0" encoding="UTF-8"?> 
  4. <beans 
  5.     xmlns="http://www.springframework.org/schema/beans" 
  6.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> 
  8.       
  9.     <bean id="greetingService" class="com.qdu.sun.spring.HelloWorld"> 
  10.         <constructor-arg> 
  11.             <value type="java.lang.String">Welcome!</value> 
  12.         </constructor-arg> 
  13.     </bean></beans> 

SpringTest.java

  1. 代码  
  2.  
  3. package com.qdu.sun.spring;  
  4.  
  5. import org.springframework.beans.factory.BeanFactory;  
  6. import org.springframework.beans.factory.xml.XmlBeanFactory;  
  7. import org.springframework.core.io.ClassPathResource;  
  8.  
  9. public class SpringTest {  
  10.      public static void main( String[] args ){  
  11.             BeanFactory factory = new XmlBeanFactory( new ClassPathResource("applicationContext.xml") );  
  12.             HelloWorld gc = (HelloWorld)factory.getBean("greetingService");  
  13.             gc.sayGreeting();  
  14.         }  

HelloWorld.java

  1. 代码  
  2.  
  3. package com.qdu.sun.spring;  
  4.  
  5. public class HelloWorld {  
  6. private String greeting;  
  7.       
  8.     public HelloWorld(){  
  9.           
  10.     }  
  11.       
  12.     public HelloWorld( String greeting ){  
  13.         this.greeting = greeting;  
  14.     }  
  15.       
  16.     public void sayGreeting(){  
  17.         System.out.println( greeting );  
  18.     }  
  19.       
  20.     public void setGreeting( String greeting ){  
  21.         this.greeting = greeting;  
  22.     }  
  23.  

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:
  1. Spring之IOC
  2. Spring IOC 理解

开发者交流群:

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

helloworld 编程实例 spring ioc

上一篇:研究微信小程序

下一篇:聚合数据iOS SDK 12306火车票查询订票演示示例

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》
开发者交流群×