Spring 4 特殊值的注入问题和各种类型的自动装配

特殊值的注入问题和各种类型的自动装配

项目结构

1.

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

	
	<bean id="teacher" class="org.lanqiao.entity.Teacher">
		<!-- <property name="name"> --><!-- <property name="name" value=""> -->
			<!-- 处理空值null  ""-->
			<!-- <null/> -->
			<!-- <value></value> -->
			<!-- <value type="java.lang.String">zxs</value> --><!-- type可写可不写 -->
			<!-- <value type="java.lang.String">z&lt;s</value> -->
			<!-- <value type="java.lang.String">z<![CDATA[<>&]]>s</value> -->
		<!-- </property> -->
		<!-- <property name="name" value="z&lt;s"></property> -->	
		<!-- 通过set方式赋值 -->
		<!-- <property name="name" value="zs"></property>
		<property name="age" value="23"></property> -->
		<!-- 通过构造器方式赋值 -->
		<constructor-arg><value>ls</value></constructor-arg>
		<constructor-arg><value>22</value></constructor-arg>
	</bean>
	<!-- autowire="byName"
		Course类中有一个ref属性teacher(属性名),并且该ioc容器中恰好有一个bean的id也是teacher
		bean的id值-类的属性名				
	 -->	
	<bean id="course" class="org.lanqiao.entity.Course" autowire="byName">
	<!-- <bean id="course" class="org.lanqiao.entity.Course" autowire="byType"> -->
	<!-- <bean id="course" class="org.lanqiao.entity.Course" autowire="constructor"> -->
		<property name="courseName" value="java"></property>
		<property name="courseHour" value="200"></property>
		<!-- <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> -->
		<constructor-arg name="listElement">
			<list>
				<value>足球x</value>
				<value>篮球x</value>
				<value>乒乓球x</value>
			</list>	
		</constructor-arg>	
		<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>
	
	<!-- <bean id="studentDao" class="org.lanqiao.dao.StudentDaoImpl">
	</bean> -->
	<!-- 配置扫描器 -->
	<context:component-scan base-package="org.lanqiao.dao"></context:component-scan>
</beans>

2.

package org.lanqiao.entity;

public class Course {
	private String courseName; 
	private int courseHour;
	private Teacher teacher;//授课老师,依赖于Teacher类
	
	
	public Course() {
	}
	public Course(Teacher teacher) {		
		this.teacher = teacher;
	}
	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()+","+this.teacher.getAge());
	}

}

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) {	
		this.listElement = listElement;
		
	}
	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;
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值