打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
spring的ioc实现和例子
2.spring的ioc容器实现
使用spring的方式,创建一个由spring ioc容器控制的ioc的应用程序。
1)定义用于接口,打印“hello,world”消息
Java代码
  1. public interface MessageProvider {   
  2.  public String getMessage();   
  3. }  

2)实现这个接口
Java代码
  1. public class MessageHello implements MessageProvider {   
  2.  public String getMessage() {   
  3.   String message = "hello,world";   
  4.   return message;   
  5.  }   
  6. }  

3)setter injection
Java代码
  1. public class HelloWorld {   
  2.  private MessageProvider provider;   
  3.   
  4.  public void setProvider(MessageProvider provider) {   
  5.   this.provider = provider;   
  6.  }   
  7.   
  8.  public void showMessage() {   
  9.   System.out.println(provider.getMessage());   
  10.  }   
  11. }  

4)配置依赖注入关系
在applicationContext.xml中配置:

Java代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">   
  3.   
  4. <beans>   
  5. <bean id="helloBean" class="test.spring.HelloWorld">   
  6. <property name="provider">   
  7.    <ref local="helloProvider"/>   
  8. </property>   
  9. </bean>   
  10. <bean id="helloProvider" class="test.spring.MessageHello"></bean>   
  11. </beans>  

5)创建测试类
Java代码
  1. public class ShowHello {   
  2. public static void main(String[] args){   
  3.  try{   
  4.  XmlBeanFactory factory =new XmlBeanFactory(new FileSystemResource("src/applicationContext.xml"));   
  5.  HelloWorld messageReader = (HelloWorld)factory.getBean("helloBean");   
  6.  messageReader.showMessage();   
  7.  }catch(Exception ex){   
  8.   System.out.println(ex.toString());   
  9.  }   
  10. }   
  11. }  

运行测试类,控制台输出:Hello,World!
 
 
 
编写第一个Spring程序
HelloWorld接口:
Java代码
  1. /**  
  2.  *   
  3.  * @Copyright(C),2009-2010 SISE Java Team.  
  4.  * @Author:easinchu  
  5.  * @Email:easinchu@gmail.com   
  6.  * @Description:  
  7.  */  
  8. public interface HelloWorld {   
  9.   
  10.     public void sayHello();   
  11. }  

HelloWorldBean实现类:
Java代码
  1. /**  
  2.  *   
  3.  * @Copyright(C),2009-2010 SISE Java Team.  
  4.  * @Author:easinchu  
  5.  * @Email:easinchu@gmail.com   
  6.  * @Description:  
  7.  */  
  8. public class HelloWorldBean implements HelloWorld{   
  9.   
  10.     private String helloWorld;   
  11.        
  12.     public void setHelloWorld(String helloWorld) {   
  13.         this.helloWorld = helloWorld;   
  14.     }   
  15.        
  16.     public void sayHello() {   
  17.         System.out.println(helloWorld);   
  18.     }   
  19. }  

Spring XML配置文件ioc-config.xml:
Xml代码
  1. <bean id="helloWorldBean" class="cn.com.sise.firstapp.HelloWorldBean">  
  2.     <property name="helloWorld">  
  3.         <value>Hello,Welcome To Spring World!</value>  
  4.     </property>  
  5. </bean>  

测试类:
Java代码
  1. import org.springframework.beans.factory.BeanFactory;   
  2. import org.springframework.beans.factory.xml.XmlBeanFactory;   
  3. import org.springframework.core.io.ClassPathResource;   
  4. import org.springframework.core.io.Resource;   
  5.   
  6. /**  
  7.  *   
  8.  *@Copyright(C),2009-2010 SISE Java Team.  
  9.  *@Author:easinchu  
  10.  *@Email:easinchu@gmail.com   
  11.  *@Description:采用Spring的BeanFactory构造IoC容器.  
  12.  */  
  13. public class FirstSpringDemo {   
  14.        
  15.     public static void main(String []args) {   
  16.         //-----------BeanFactory IoC容器---------------------//   
  17.         //从classpath路径上装载XML的配置信息   
  18.         Resource resource = new ClassPathResource("ioc-config.xml");   
  19.            
  20.         //实例化IOC容器,此时容器并未实例化beans-config.xml所定义的各个受管bean.   
  21.         BeanFactory factory = new XmlBeanFactory(resource);   
  22.            
  23.         /   
  24.            
  25.         //获取受管bean   
  26.         HelloWorld hello = (HelloWorld)factory.getBean("helloWorldBean");   
  27.         hello.sayHello();   
  28.     }   
  29. }  

步骤小结
①利用XmlBeanFactory读取xml配置文件并建立BeanFactory实例
②BeanFactory依据配置文件完成依赖注入
③通过getBean()方法指定Bean名称取得Bean实例


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
IOC模式与JAVA反射机制
Spring学习笔记:第一章 Spring的下载和安装
Spring控制反转(IoC)的理解
Java干货:分享Spring框架之IOC的基本配置
在集成Spring Axis 的环境下webservice的发布和部署 - Aflye...
最新分享
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服