Chinaunix首页 | 论坛 | 博客
  • 博客访问: 203246
  • 博文数量: 73
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 750
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-13 18:32
文章分类

全部博文(73)

文章存档

2009年(1)

2008年(72)

我的朋友

分类: Java

2008-03-19 10:35:20

  一个简单的Spring程序。

#javabean文件
package com.biaoflying;

public class HelloBean {
    private String helloWord;
    
    public void setHelloWord(String helloWord){
        this.helloWord=helloWord;
    }
    public String getHelloWord(){
        return helloWord;
    }

}

#SpringDemo.java主程序
package com.biaoflying;

import org.springframework.core.io.*;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.xml.*;

public class SpringDemo {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Resource rs=new              FileSystemResource("beans-config.xml");
        BeanFactory factory=new XmlBeanFactory(rs);
       
        HelloBean      hello=(HelloBean)factory.getBean("helloBean");
        System.out.println(hello.getHelloWord());
    }

}

  所需要的jar包,spring.jar,spring-beans.jar,commons-logging.jar

#beans-config.xml文件

 "">
 

    
        
            hello!I'm back
        

    


  输出

hello!I'm back

----------------------------------------------------------------------------------
 
  以上使用的是Setter injection方式实现依赖注入,下面是用Constructor injection.

package com.biaoflying;

import org.springframework.context.*;
import org.springframework.context.support.*;

public class SpringDemo2 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext context=
            new FileSystemXmlApplicationContext(
                    "beans-config2.xml");
        
        HelloBean2 hello=(HelloBean2)context.getBean("helloBean2");
        
        System.out.print("Name: ");
        System.out.println(hello.getName());
        System.out.print("Word: ");
        System.out.println(hello.getHelloWord());
    }

}

package com.biaoflying;

public class HelloBean2 {
    private String name;
    private String helloWord;
    public HelloBean2(){
    }
    public HelloBean2(String name,String helloWord){
        this.name=name;
        this.helloWord=helloWord;
    }
    
    public void setName(String name){
        this.name=name;
    }
    public String getName(){
        return name;
    }
    public void setHelloWord(String helloWord){
        this.helloWord=helloWord;
    }
    public String getHelloWord(){
        return helloWord;
    }
}

#beans-config2.xml文件

 "">
 

    
        
            Justin
        

        
            Hello
        

    


#输出:
Name: Justin
Word: Hello

-----------------------------------------------------------------------------------

  属性参考

package com.biaoflying;

import org.springframework.context.*;
import org.springframework.context.support.*;

public class RefBeanDemo {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext context=
            new FileSystemXmlApplicationContext(
                    "beans-config.xml");
        HelloBean hello=
            (HelloBean)context.getBean("helloBean");
        System.out.println(hello.getHelloWord());
        System.out.print(" it's ");
        System.out.println(hello.getDate());
        System.out.println(".");
    }

}

package com.biaoflying;

import java.util.*;

public class HelloBean {
    private String helloWord;
    private Date date;
    
    public void setHelloWord(String helloWord){
        this.helloWord=helloWord;
    }
    public String getHelloWord(){
        return helloWord;
    }
    
    public void setDate(Date date){
        this.date=date;
    }
    public Date getDate(){
        return date;
    }
}


 "">
 

    
    
        
            hello!I'm back
        

        
            
        

    


#输出
hello!I'm back
 it's Wed Mar 19 10:51:56 CST 2008

-----------------------------------------------------------------------------------

  使用自动绑定功能

#byType实现自动绑定
#beans-config2.xml

 "">
 

    
          autowire="byType">
        
            hello!I'm back
        

    


修改主程序中的可以得到同样的输出

ApplicationContext context=
            new FileSystemXmlApplicationContext(
                    "beans-config2.xml");

#byName实现自动绑定

 "">
 

   
          autowire="byName">
       
            hello!I'm back
       

   



#constructor自动绑定
#在HelloBean文件中加入以下代码
  public HelloBean(Date date){
    this.date=date;
  }
#修改beans-config2.xml文件

 "">
 

   
          autowire="constructor">
       
            hello!I'm back
       

   


 
  另外还可以将autowire设定为”autodetect“,spring会尝试使用constructor,byType,byName等方式建立以来关系。

  使用自动绑定时,并不能从文件中清楚地看出依赖关系是否建立,可以在bean标签中加入"dependency-check",可以设定为simple(检查原语类型) , objects(检查对象类型), all(simple+objects) ,none。

阅读(823) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~