Spring中2种依赖注入的方式

一:依赖注入的方式
  constructor-arg:通过构造函数注入。
  property:通过setxx方法注入。

二:constructor-arg的简单使用

  java代码

<span style="font-size:18px;">    public class Man {

	private String name ;
	private int age;
	private List hobby;
	private Map  friends;
	private Set  set;
	private boolean ifMarried;
	
	public Man() {
		
	}
	
    public Man(String name, int age,List hobby,Map friends,Set    set,boolean ifMarried){
    	this.name = name;
    	this.age = age;
    	this.hobby = hobby;
    	this.friends = friends;
    	this.set = set;
    	this.ifMarried = ifMarried;
    }
    
    public String getInfo(){
    	
    	String info = "姓名:"+this.name+"\n年龄:"+this.age+"\n爱好:"+this.hobby+"\n朋友:"+this.friends+"\n婚否:"+this.ifMarried+"\n其他的:"+this.set;
        return info;
    }

}
 
  </span>

xml配置文件

<span style="font-size:18px;">	<bean id="man" class="com.spring.test.man.Man">
	   <constructor-arg value="zzy" index="0" >
	   </constructor-arg>
	   
	   <constructor-arg value="10" index="1">
	   </constructor-arg>
	  
	   <constructor-arg>
	     <list>
	        <value>movie</value>
	        <value>music</value>
	     </list>
	   </constructor-arg>
	   
	   <constructor-arg>
	      <set>
	         <value>Lady is GaGa</value>
	         <value>GaGa is Lady</value>
	      </set>
	   </constructor-arg>

	   <constructor-arg>
	       <map>
	          <entry key="liuhua" value="man"></entry>
	          <entry key="xujinglei" value="female"></entry>
	       </map>
	   </constructor-arg>
	   
	   <constructor-arg index="5" value="0">
	   </constructor-arg>
	</bean></span>

最后一个参数ifMarried是一个boolean类型的参数,在配置的时候可以是true/false或者0/1;

二:property的简单使用

<span style="font-size:18px;">public class Doctor {

	private String name;
	private String sex;
	
	
	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}

	

	public String getSex() {
		return sex;
	}


	public void setSex(String sex) {
		this.sex = sex;
	}


	public void init(){
		System.out.println("88888888888");
	}
	public void init(String name,String sex){
		this.name = name;
		this.sex = sex;
	}
}</span>

xml配置文件:

<span style="font-size:18px;">  <bean id="doctor" class="com.spring.test.man.Doctor" init-method="init">
	  <property name="name" value="doctor"></property>
	  <property name="sex" value="i don't know"></property>
	</bean></span>



转载自:http://zzy7182.iteye.com/blog/1153473









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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值