1. 程式人生 > >Spring框架 ref 和idref的區別

Spring框架 ref 和idref的區別

Spring是一個開放原始碼的設計層面框架,他解決的是業務邏輯層和其他各層的鬆耦合問題,因此它將面向介面的程式設計思想貫穿整個系統應用。

idref元素用來將容器內其它beanid傳給<constructor-arg/>  <property/>元素,同時提供錯誤驗證功能,減少配置的書寫錯誤機率。除了<idref bean=""/>,如果被引用的bean在同一個xml檔案中,且bean的名字就是beanid,除了可以使用<idref local=""/>,此屬性允許xml解析器在解析XML的時候對引用的bean進行驗證。其實idref就跟value一樣,只是將某個字串注入到屬性或者建構函式中,只不過注入的是某個bean定義的id屬性值。

	<bean id="card" name="card" class="com.zking.pojo.Card">

		<constructor-arg index="0" value="121212121"></constructor-arg>
		<constructor-arg index="1" value="密碼"></constructor-arg> 
		
	</bean>
     

   <bean id="card" name="card" class="com.zking.pojo.Card">

	<property name="cid" value="2323"></property>
	<property name="cname" value="ls"></property>

	</bean>
<bean id="person" class="com.zking.pojo.Person">

     <property name="person" value="person" />

</bean>

同樣在<constructor-arg/>或者<property/>元素內部也可以使用ref元素。用來將bean中指定屬性的值設定為對容器中的另外一個bean的引用。 

<bean id="person" class="com.zking.pojo.Person">
    <property name="person">
        <ref bean="person" />
    </property>
package com.zking.pojo;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class Person {
	private String pid;
	private String pname;
	private String pwd;

	private Map<Object, Object> lm;

	private Set<String> sc;
	private Set<Card> lsc;

	private List<String> lp;

	private Card card;

	
	
	public Set<Card> getLsc() {
		return lsc;
	}

	public void setLsc(Set<Card> lsc) {
		this.lsc = lsc;
	}

	public Map<Object, Object> getLm() {
		return lm;
	}

	public void setLm(Map<Object, Object> lm) {
		this.lm = lm;
	}

	public Set<String> getSc() {
		return sc;
	}

	public void setSc(Set<String> sc) {
		this.sc = sc;
	}

	public List<String> getLp() {
		return lp;
	}

	public void setLp(List<String> lp) {
		this.lp = lp;
	}

	public Card getCard() {
		return card;
	}

	public void setCard(Card card) {
		this.card = card;
	}

	public Person() {
		super();
	}

	public Person(String pid, String pname, String pwd) {
		super();
		this.pid = pid;
		this.pname = pname;
		this.pwd = pwd;
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public String getPname() {
		return pname;
	}

	public void setPname(String pname) {
		this.pname = pname;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	@Override
	public String toString() {
		return "Person [pid=" + pid + ", pname=" + pname + ", pwd=" + pwd + "]";
	}
	
	
	//init-method
	public void infName() {
		System.out.println("初始化的值");
	}

}
package com.zking.pojo;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.jws.Oneway;

/**
 * @ClassName: Card
 * @Description: (描述這個類的作用)
 * @author LJY
 * @date 2018年9月17日 下午2:59:22
 *
 */
public class Card {

	private String cid;
	private String cname;

	private Set<String> ls;

	private List<String> lp;

	private Map<Object, Object> lm;

	private Person person;

	public Card() {
		super();
	}

	public Map<Object, Object> getLm() {
		return lm;
	}

	public void setLm(Map<Object, Object> lm) {
		this.lm = lm;
	}

	public Set<String> getLs() {
		return ls;
	}

	public void setLs(Set<String> ls) {
		this.ls = ls;
	}

	public List<String> getLp() {
		return lp;
	}

	public void setLp(List<String> lp) {
		this.lp = lp;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public Card(String cid, String cname) {
		super();
		this.cid = cid;
		this.cname = cname;
	}

	public String getCid() {
		return cid;
	}

	public void setCid(String cid) {
		this.cid = cid;
	}

	public String getCname() {
		return cname;
	}

	public void setCname(String cname) {
		this.cname = cname;
	}

	@Override
	public String toString() {
		return "Card [cid=" + cid + ", cname=" + cname + "]";
	}

}

區別:

ref:注入的是bean的例項 
idref:注入的是string。