lm高会影响卵泡质量吗:spring的ioc实现和例子

来源:百度文库 编辑:偶看新闻 时间:2024/04/30 21:07:49
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. "1.0" encoding="UTF-8"?>   
  2. "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">   
  3.   
  4.   
  5. "helloBean" class="test.spring.HelloWorld">   
  6. "provider">   
  7.    "helloProvider"/>   
  8.   
  9.   
  10. "helloProvider" class="test.spring.MessageHello">   
  11.   

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.  id="helloWorldBean" class="cn.com.sise.firstapp.HelloWorldBean">  
  2.      name="helloWorld">  
  3.         Hello,Welcome To Spring World!  
  4.       
  5.   

测试类:
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实例