Spring 构造参数的注入方式

HelloBean.java

java 代码
  1. package com.gjx.spring;   
  2.   
  3. import java.util.Date;   
  4.   
  5. public class HelloBean {   
  6.     private String name;   
  7.   
  8.     private String helloWord;   
  9.        
  10.     private Date date;   
  11.   
  12.     public HelloBean() {   
  13.     }   
  14.     public HelloBean(String helloWord,Date date){   
  15.         this.helloWord=helloWord;   
  16.         this.date=date;   
  17.     }   
  18.        
  19.     public HelloBean(String name, String helloWord) {   
  20.         this.name = name;   
  21.         this.helloWord = helloWord;   
  22.     }   
  23.   
  24.     public String getHelloWord() {   
  25.         return helloWord;   
  26.     }   
  27.   
  28.     public void setHelloWord(String helloWord) {   
  29.         this.helloWord = helloWord;   
  30.     }   
  31.   
  32.     public String getName() {   
  33.         return name;   
  34.     }   
  35.   
  36.     public void setName(String name) {   
  37.         this.name = name;   
  38.     }   
  39.     public Date getDate() {   
  40.         return date;   
  41.     }   
  42.     public void setDate(Date date) {   
  43.         this.date = date;   
  44.     }   
  45.   
  46. }   

hellobean-config.xml

xml 代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">  
  3.   
  4. <beans>  
  5.     <!-- 默认的情况下是按照 参数的顺序 注入 当指定index索引后就可以改变注入参数的顺序 -->  
  6.     <bean id="helloBean" class="com.gjx.spring.HelloBean">  
  7.         <constructor-arg index="0">  
  8.             <value>冬国</value>  
  9.         </constructor-arg>  
  10.         <constructor-arg index="1">  
  11.             <value>你好 </value>  
  12.         </constructor-arg>  
  13.     </bean>  
  14.        
  15.     <!-- 当构造函数的参数相同时,Spring会自动找到配制的构造方法 也可以用 type 来明确指定要用那个类型-->  
  16.     <bean id="date" class="java.util.Date" />  
  17.     <bean id="helloBean2" class="com.gjx.spring.HelloBean">  
  18.         <constructor-arg index="0">  
  19.             <value>哈哈</value>  
  20.         </constructor-arg>  
  21.         <constructor-arg index="1" type="java.util.Date">  
  22.             <ref bean="date"/>  
  23.         </constructor-arg>  
  24.     </bean>    
  25.        
  26.        
  27. </beans>  

测试

 

java 代码
  1. package Test.com.gjx.spring;   
  2.   
  3. import java.util.Date;   
  4.   
  5. import junit.framework.TestCase;   
  6.   
  7. import org.springframework.beans.factory.BeanFactory;   
  8. import org.springframework.beans.factory.xml.XmlBeanFactory;   
  9. import org.springframework.context.ApplicationContext;   
  10. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  11. import org.springframework.core.io.ClassPathResource;   
  12. import org.springframework.core.io.Resource;   
  13.   
  14. import com.gjx.spring.HelloBean;   
  15. import com.gjx.spring.SomeBean;   
  16.   
  17. public class TestHelloBean extends TestCase {   
  18.     /*  
  19.      * BeanFactory负责读取Bean的定义文件;管理对象的加载。生成。维护Bean对象与Bean对象之间的依赖关系  
  20.      * 但只适合简单的应用程序  
  21.      */  
  22.     public void testHelloWord() {   
  23.         Resource rs = new ClassPathResource("hellobean-config.xml");   
  24.         BeanFactory factory = new XmlBeanFactory(rs);   
  25.         HelloBean hello = (HelloBean) factory.getBean("helloBean");   
  26.         HelloBean hello2 = (HelloBean) factory.getBean("helloBean2");   
  27.            
  28.         System.out.println(hello.getHelloWord()+"  "+hello.getName());   
  29.            
  30.         System.out.println(hello2.getHelloWord()+"  "+(Date)hello2.getDate());   
  31.     }   
  32. }  

结果:

你好   冬国
哈哈  Fri Aug 17 15:10:23 CST 2007

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值