Spring review--常用屬性String、int、list、set、Map的注入

ZeroWM發表於2016-08-07

依賴注入

  元件不做定位查詢,元件依賴關係、裝配全部交給容器全權負責。容器會把符合依賴關係的物件通過Java屬性或者建構函式傳遞給所需要的物件。


常用屬性String、int、list、set、Map的注入方法如下:

applicationContext.xml

<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	     xmlns:tx="http://www.springframework.org/schema/tx"
	     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

	<bean id="bean1" class="com.bjpowernode.spring.Bean1">
		<property name="strValue" value="hello"/>
		 <property name="intValue" value="123"/>
		<property name="listH" >
			<list>
				<value>list1</value>
				<value>list2</value>
			</list>
		</property>  
		<property name="setValue">
			<set>
				<value>set1</value>
				<value>set2</value>
			</set>
		</property>

		<property name="mapValueMap" >
			<map>
				<entry key="k1" value="v1"/>
				<entry key="k2" value="v2"/>
			</map>
		</property>
	
	</bean>

</beans>
	</span>



Bean1.java

<span style="font-size:14px;">package com.bjpowernode.spring;

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

public class Bean1 {
	private String strValue;
	private int intValue;
	private List listH;	
	private Set setValue;
	private String[] arrayValue;
	private Map mapValueMap;
	
	
	public String getStrValue() {
		return strValue;
	}
	public void setStrValue(String strValue) {
		this.strValue = strValue;
	}
	public int getIntValue() {
		return intValue;
	}
	public void setIntValue(int intValue) {
		this.intValue = intValue;
	}

	public List getListH() {
		return listH;
	}
	public void setListH(List listH) {
		this.listH = listH;
	}
	public Set getSetValue() {
		return setValue;
	}
	public void setSetValue(Set setValue) {
		this.setValue = setValue;
	}
	public String[] getArrayValue() {
		return arrayValue;
	}
	public void setArrayValue(String[] arrayValue) {
		this.arrayValue = arrayValue;
	}
	public Map getMapValueMap() {
		return mapValueMap;
	}
	public void setMapValueMap(Map mapValueMap) {
		this.mapValueMap = mapValueMap;
	}
	
}
</span>



InjectitonTest.java

<span style="font-size:14px;">package Test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bjpowernode.spring.Bean1;

import junit.framework.TestCase;

public class InjectionTest extends TestCase {

	private BeanFactory factory;
	@Override
	protected void setUp() throws Exception {
		 factory =new ClassPathXmlApplicationContext("applicationContext.xml");//獲取xml檔案裡面的bean資訊
	}

	@Override
	protected void tearDown() throws Exception {
		// TODO Auto-generated method stub
		super.tearDown();
	}
	
	public void testInjecton1(){
		Bean1 baBean1=(Bean1)factory.getBean("bean1");
		System.out.println("bean1.strValue="+baBean1.getStrValue());
		System.out.println("bean1.getIntValue="+baBean1.getIntValue());
		System.out.println("bean1.getListValue="+baBean1.getListH());
		System.out.println("bean1.getSetValue="+baBean1.getSetValue());
		System.out.println("bean1.getArrayValue="+baBean1.getArrayValue());
		System.out.println("bean1.getMapValueMap="+baBean1.getMapValueMap());
		
	}

}

</span>



相關文章