温馨提示×

温馨提示×

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

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

spring IoC编程实例

发布时间:2020-04-09 19:03:27 来源:网络 阅读:355 作者:linzheng 栏目:开发技术

配置文件

/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.  

 

向AI问一下细节

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

AI