4.spring di

spring di,即依賴注入,從應用的淺顯意義來說就是對屬性賦值html

1.用setter賦值,在spring的applicationContext.xml配置文件的bean下的property標籤java

  屬性name指定屬性名,屬性value指定值,通常用於基本數據 類型的包裝類型spring

 屬性ref指定值,通常用於引用類型,還有list標籤,下面value標籤,app

set標籤下面value標籤,map標籤下面entry,下面分別有key,value,函數

還有props,下面prop,keythis

public class Student {
	public void say(){
		System.out.println("student");
	}
}
public class Person {
	private Long pid;//包裝類型
	private String pname;//String類型
	private Student student;//引用類型
	
	private List lists;
	
	private Set sets;
	
	public Long getPid() {
		return pid;
	}

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

	public String getPname() {
		return pname;
	}

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

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	public List getLists() {
		return lists;
	}

	public void setLists(List lists) {
		this.lists = lists;
	}

	public Set getSets() {
		return sets;
	}

	public void setSets(Set sets) {
		this.sets = sets;
	}

	public Map getMap() {
		return map;
	}

	public void setMap(Map map) {
		this.map = map;
	}

	public Properties getProperties() {
		return properties;
	}

	public void setProperties(Properties properties) {
		this.properties = properties;
	}

	private Map map;
	
	private Properties properties;
}

  

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    <bean id="person" class="cn.di.xml.set.Person">
    	<!-- 
    		property就是表明屬性
    		  在spring中基本類型(包裝類型和String)均可以用value來賦值
    		                         引用類型用ref賦值
    	 -->
    	<property name="pid" value="5"></property>
    	<property name="pname" value="王五"></property>
    	<property name="student">
    		<ref bean="student"/>
    	</property>
    	<property name="lists">
    		<list>
    			<value>list1</value>
    			<value>list2</value>
    			<ref bean="student"/>
    		</list>
    	</property>
    	<property name="sets">
    		<set>
    			<value>set1</value>
    			<value>set2</value>
    			<ref bean="student"/>
    		</set>
    	</property>
    	<property name="map">
    		<map>
    			<entry key="map1">
    				<value>map1</value>
    			</entry>
    			<entry key="map2">
    				<value>map2</value>
    			</entry>
    			<entry key="map3">
    				<ref bean="student"/>
    			</entry>
    		</map>
    	</property>
    	<property name="properties">
    		<props>
    			<prop key="prop1">
    				prop1
    			</prop>
    		</props>
    	</property>
    </bean>
	<bean id="student" class="cn.di.xml.set.Student"></bean>
</beans>

  

@Test
	public void test(){
               ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		Person person = (Person)context.getBean("person");
		person.getStudent().say();
		System.out.println(person.getPid());
		System.out.println(person.getPname());
		List lists = person.getLists();
		for(int i=0;i<lists.size();i++){
			System.out.println(lists.get(i).toString());
		}
	}

  

2.利用構造函數賦值xml

在bean下有constructor-arg標籤,裏面有index,type,ref,value屬性htm

public class Person {
	private Long pid;
	public Long getPid() {
		return pid;
	}

	public String getPname() {
		return pname;
	}

	public Student getStudent() {
		return student;
	}

	private String pname;
	private Student student;
	
	public Person(Long pid,String pname){
		this.pid = pid;
		this.pname = pname;
	}
	
	public Person(String pname,Student student){
		this.pname = pname;
		this.student = student;
	}
}

  

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    <bean id="person" class="cn.di.xml.constructor.Person">
    	<!-- 
    		構造函數的參數
    		  index  第幾個參數,下標從0開始
    		  type   參數的類型
    		  ref    若是類型是引用類型,賦值
    		  value  若是類型是基本類型,賦值
    		 說明:
    		    只能指定一個構造函數
    	 -->
    	<constructor-arg index="0"  type="java.lang.String" value="王五"></constructor-arg>
    	<constructor-arg index="1"  ref="student"></constructor-arg>
    </bean>
    
    <bean id="student" class="cn.di.xml.constructor.Student"></bean>
</beans>
相關文章
相關標籤/搜索