Spring 3 三种方式的依赖注入给各种集合类型的属性注入

三种方式的依赖注入给各种集合类型的属性注入

学习于https://study.163.com/course/courseLearn.htm?courseId=1005991005#/learn/video?lessonId=1053365080&courseId=1005991005

项目结构

1.

package org.lanqiao.entity;

public class Teacher {
	private String name ;
	private int age;
	
	public Teacher() {
	}
	public Teacher(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

2.

package org.lanqiao.entity;

public class Course {
	private String courseName; 
	private int courseHour;
	private Teacher teacher;//授课老师,依赖于Teacher类
	
	
	public Course() {
	}
	public Course(String courseName, int courseHour, Teacher teacher) {
		super();
		this.courseName = courseName;
		this.courseHour = courseHour;
		this.teacher = teacher;
	}
	
	public String getCourseName() {
		return courseName;
	}
	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}
	public int getCourseHour() {
		return courseHour;
	}
	public void setCourseHour(int courseHour) {
		this.courseHour = courseHour;
	}
	public Teacher getTeacher() {
		return teacher;
	}
	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

	public void showInfo() {
		System.out.println(this.courseName+"," +this.courseHour+","+this.teacher.getName());
	}
}

3.

package org.lanqiao.entity;

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

public class AllCollectionType {
	private List<String> listElement ;
	private String[] arrayElement ;
	private Set<String> setElement ;
	private Map<String,String> mapElement;
	private Properties propsElement ;
	
	
	public AllCollectionType() {
	}
	public AllCollectionType(List<String> listElement, String[] arrayElement, Set<String> setElement,
			Map<String, String> mapElement, Properties propsElement) {
		super();
		this.listElement = listElement;
		this.arrayElement = arrayElement;
		this.setElement = setElement;
		this.mapElement = mapElement;
		this.propsElement = propsElement;
	}
	
	public List<String> getListElement() {
		return listElement;
	}
	public void setListElement(List<String> listElement) {
		this.listElement = listElement;
	}
	public String[] getArrayElement() {
		return arrayElement;
	}
	public void setArrayElement(String[] arrayElement) {
		this.arrayElement = arrayElement;
	}
	public Set<String> getSetElement() {
		return setElement;
	}
	public void setSetElement(Set<String> setElement) {
		this.setElement = setElement;
	}
	public Map<String, String> getMapElement() {
		return mapElement;
	}
	public void setMapElement(Map<String, String> mapElement) {
		this.mapElement = mapElement;
	}
	public Properties getPropsElement() {
		return propsElement;
	}
	public void setPropsElement(Properties propsElement) {
		this.propsElement = propsElement;
	}
	
	@Override
	public String toString() {
		String strContent="";
		for(String str:arrayElement) {
			strContent+=str+",";
		}
		return "list:"+this.listElement+"\nset:"+this.setElement+"\nmap:"+this.mapElement+"\nprops:"+this.propsElement+"\narray:"+strContent;
	}
}

4.

<?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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="teacher" class="org.lanqiao.entity.Teacher" p:age="25" p:name="ww">
		<!-- 通过set方式赋值 -->
		<!-- <property name="name" value="zs"></property>
		<property name="age" value="23"></property> -->
		<!-- 通过构造器方式赋值 -->
		<!-- <constructor-arg value="ls"></constructor-arg>
		<constructor-arg value="24"></constructor-arg> -->
		
		<!-- <constructor-arg value="24" index="1"></constructor-arg>
		<constructor-arg value="ls" index="0"></constructor-arg> -->
		<!-- <constructor-arg value="24" name="age"></constructor-arg>
		<constructor-arg value="ls" name="name"></constructor-arg> -->
		<!-- <constructor-arg value="24" type="int" index="1" name="age"></constructor-arg>
		<constructor-arg value="ls" type="String" index="0" name="name"></constructor-arg> -->
	</bean>
	<bean id="course" class="org.lanqiao.entity.Course" p:courseHour="300" p:courseName="hadoop" p:teacher-ref="teacher">
		<!--xx. setCourseName( "java")
			courseName ->setCourseName()
		-->		
		<!-- <property name="courseName" value="java"></property>
		<property name="courseHour" value="200"></property> -->
		<!-- 将teacher对象注入到course对象中
				xx. setTeacher(teacher);
		-->
		<!-- <property name="teacher" ref="teacher"></property> -->
		<!-- <constructor-arg value="c"></constructor-arg>
		<constructor-arg value="100 "></constructor-arg>
		<constructor-arg ref="teacher"></constructor-arg> -->		
	</bean>

	<bean id="collectionDemo" class="org.lanqiao.entity.AllCollectionType">
		<!--通过set方式赋值-->
		<property name="listElement">
			<list>
				<value>足球1</value>
				<value>篮球1</value>
				<value>乒乓球1</value>
			</list>			
		</property>
		<property name="arrayElement">
			<array>
				<value>足球2</value>
				<value>篮球2</value>
				<value>乒乓球2</value>
			</array>
		</property>
		<property name="setElement">
			<set>
				<value>足球3</value>
				<value>篮球3</value>
				<value>乒乓球3</value>
			</set>
		</property>
		<property name="mapElement">
			<map>
				<entry>
					<key>
						<value>foot4</value>
					</key>
					<value>足球4</value>
				</entry>
				<entry>
					<key>
						<value>basketball4</value>
					</key>
					<value>篮球4</value>
				</entry>
				<entry>
					<key>
						<value>ping-pang4</value>
					</key>
					<value>乒乓球4</value>
				</entry>				
			</map>
		</property>
		<property name="propsElement">
			<props>
				<prop key="foot5">足球5</prop>
				<prop key="basketball5">篮球5</prop>
				<prop key="ping-pang5">乒乓球5</prop>
			</props>		
		</property>
	</bean>
</beans>

5.

package org.lanqiao.test;

import org.lanqiao.entity.AllCollectionType;

import org.lanqiao.entity.Course;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
	public static void testDI() {
		ApplicationContext conext=new ClassPathXmlApplicationContext("applicationContext.xml");		
		Course course =(Course)conext.getBean("course");
		course.showInfo();
	}
	private static void collectionDemo() {
		// TODO Auto-generated method stub	
		ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
		AllCollectionType type=(AllCollectionType)context.getBean("collectionDemo");
		System.out.println(type);
	}
	public static void main(String[] args) {
		testDI();
		//collectionDemo();
	}
}

7.结果

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值