Spring4.0 xml显示配置(构造器参数注入,属性注入)

项目目录:

Spring4.0 xml显示配置(构造器参数注入,属性注入)_第1张图片

package paraBean;

public class AnoBean {
	
	void play(){
		System.out.println("i am abbean");
	}

}
package paraBean;



public class CtBean {

	void play(){
		System.out.println("i am ctbean");
	}

}

package paraBean;

public interface Disk {
	void play();
}
package paraBean;

import java.util.List;

public class JayDiskImpl implements Disk {
	
//注入bean,字符串,集合三种类型
	private CtBean cb;
	private AnoBean ab;
	private String str;
	private Liststrlist;
	private Listblist;
 //以上用于构造器注入属性
	
 //以下用于setter属性注入	
	private CtBean cbn;
	private String setStr;
	private ListsetStrlist;
	private ListsetObjlist;
	
	public CtBean getCbn() {
		return cbn;
	}

	public void setCbn(CtBean cbn) {
		this.cbn = cbn;
	}

	public String getSetStr() {
		return setStr;
	}

	public void setSetStr(String setStr) {
		this.setStr = setStr;
	}

	public List getSetStrlist() {
		return setStrlist;
	}

	public void setSetStrlist(List setStrlist) {
		this.setStrlist = setStrlist;
	}

	public List getSetObjlist() {
		return setObjlist;
	}

	public void setSetObjlist(List setObjlist) {
		this.setObjlist = setObjlist;
	}

	public JayDiskImpl(CtBean cb,AnoBean ab,String str,Liststrlist,Listblist){
		this.cb=cb;
		this.ab=ab;
		this.str=str;
		this.strlist=strlist;
		this.blist=blist;
	}
	
	public void play() {
		cb.play();
		ab.play();
		System.out.println("I am anotation "+str);
		for(int i=0;i 
  


package paraBean;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

	
		public static void main(String[] args) {
		ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");
				ac.getBean("jd", JayDiskImpl.class).play();
				


		}

}



 	
 	
 	
 	
 	
 	
 		
 		
 		
 		
 			
 				我是注入集合1
 				我是注入集合2
 			
 		
 		
 			
 				
 				
 			
 		
 		
 		
 		
 		
 			
 			 属性字符串1
 			 属性字符串2
 			
 		
 		
 			
 			
 			
 			
 		
 	

 

运行test 控制台输出:

i am ctbean
i am abbean
I am anotation 注入的字符串
我是注入集合1
我是注入集合2
paraBean.AnoBean@40c06c8b
paraBean.CtBean@19163ae3
-----------------------------
-----------------------------
i am ctbean
i am 属性字符串
属性字符串1
属性字符串2
paraBean.AnoBean@40c06c8b
paraBean.CtBean@19163ae3


现在xml中的配置用c 和p标签分别来代替 construct 和property 属性标签:

此时xml头部为

  xmlns:c="http://www.springframework.org/schema/c"
  xmlns:p="http://www.springframework.org/schema/p"
  xmlns:util="http://www.springframework.org/schema/util"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd 
  http://www.springframework.org/schema/util
  http://www.springframework.org/schema/util/spring-util.xsd">

c和p标签无法像construct和property 那样直接嵌入集合,那么就使用一种折中的方法,将list 转成bean对象,然后在c和p标签中直接引用:

Spring4.0 xml显示配置(构造器参数注入,属性注入)_第2张图片

好现在来看看整个配置文件:



 	
 	
 	
 	
 	
 	
 		注入集合1
 		注入集合2
 	
 	
 		
 		
 	
 	
 	
 		
 	

 


最后输出:

i am ctbean
i am abbean
I am anotation c string
注入集合1
注入集合2
paraBean.AnoBean@75227d3
paraBean.CtBean@22b49166
-----------------------------
-----------------------------
i am ctbean
i am p string
注入集合1
注入集合2
paraBean.AnoBean@75227d3
paraBean.CtBean@22b49166

你可能感兴趣的:(spring)