Spring学习笔记之注入

         Spring3.2.8 注入

1.Xml基本约束

  

<?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.xsd">

 

 2.1构造方法注入

   构造器注入可以根据参数索引注入、参数类型注入,使用constructor-arg

   使用构造器注入通过配置构造器参数实现,构造器参数就是依赖。除了构造器方式,还有静态工厂、实例工厂方法可以进行构造器注入。

   基本注入

   按构造器参数类型注入 

<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg type="int" value="7500000"/>
<constructor-arg type="java.lang.String" value="42"/>
</bean>

    2.1.1注入日期

public class Student {
	private int age;
	private Date birth;

	Student(int age, Date birth) {
		this.age = age;
		this.birth = birth;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Date getBirth() {
		return birth;
	}
}

    声明一个 CustomDateEditor 类将字符串转换成 java.util.Date。

<bean id="dateEditor" class="org.springframework.beans.propertyeditors.CustomDateEditor">
		<constructor-arg>
			<bean class="java.text.SimpleDateFormat">
			    <constructor-arg value="yyyy-MM-dd" />
			</bean>
		</constructor-arg>
		<constructor-arg value="true" />
	</bean>

  

   声明另一个“CustomEditorConfigurer”,使 Spring转换 bean 属性,其类型为java.util.Date

<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
		<property name="customEditors">
			<map>
				<entry key="java.util.Date">
					<ref local="dateEditor" />
				</entry>
			</map>
		</property>
	</bean>

  

     注入:

<bean id="student" class="com.spring.Student">
		<constructor-arg type="int" value="24"></constructor-arg>
		<constructor-arg index="1" value="2017-04-25"></constructor-arg>
	</bean>

  2.2集合注入

集合元素

用途
  <list> 装配list类型的值,允许重复
 <set> 装配set类型的值,不允许重复
 <map> 装配map类型的值,名称和值可以是任意类型
<props> 装配<properties>类型的值,名称和值必须是string类型

   

    例用<list>来装配集合与数组

     Student类

package com.spring.collection;

public class Student {

	private String name;

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

	public String getName() {
		return name;
	}
}

    

     Person类中加入student的引用集合,String数组xh

package com.spring.collection;

import java.util.List;

public class Person {
	private List<Student> student;
	private String[] xh;

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

	public List<Student> getStudent() {
		return student;
	}

	public String[] getXh() {
		return xh;
	}

	public void setXh(String[] xh) {
		this.xh = xh;
	}
}

    配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p" 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.xsd">

	<bean id="student1" class="com.spring.collection.Student" p:name="张三" />
	<bean id="student2" class="com.spring.collection.Student" p:name="张三2" />
	<bean id="student3" class="com.spring.collection.Student" p:name="张三3" />

	<bean id="person" class="com.spring.collection.Person">
		<property name="student">
			<list>
				<ref bean="student1" />
				<ref bean="student2" />
			</list>
		</property>
		
		<property name="xh">
			<list>
			  <value>1001</value>
			  <value>1002</value>
			</list>
		</property>
	</bean>
</beans>

   注入Map

    Student类同上。

   

   Person类

public class Person {
   private Map<String,Student> mapStudent;

   public Map<String, Student> getMapStudent() {
		return mapStudent;
	}

	public void setMapStudent(Map<String, Student> mapStudent) {
		this.mapStudent = mapStudent;
	}
}

    配置文件

    

<bean id="person" class="com.spring.collection.Person">
  <property name="mapStudent">
		<map>
		      <entry key="1001" value-ref="student1"></entry>
		      <entry key="1002" value-ref="student2"></entry>
		</map>
  </property>
</bean>

   测试<list><map>类

   

public class DICollectionTest {
	public static void main(String[] args) {
		ApplicationContext ap = new ClassPathXmlApplicationContext("com/spring/collection/bean.xml");
		Person p = (Person)ap.getBean("person");
		String[] xh = p.getXh();
		System.out.println(xh[0] +"--"+xh[1]);
		List<Student> students = p.getStudent();
		for(Student student:students) {
			System.out.println(student.getName());
		}
		
		Map<String,Student> mapStudent = p.getMapStudent();
		Iterator<Entry<String,Student>> it = mapStudent.entrySet().iterator();
		while(it.hasNext()) {
			Entry<String,Student> entr= it.next();
			System.out.println(entr.getKey() + "--" +entr.getValue());
		}
	}
}

   

猜你喜欢

转载自ftime-water.iteye.com/blog/2371143