spring总结3——依赖注入

 

目录

1、给普通字段注入值

​2、通过构造方法给参数赋值以及调用指定的构造方法 

​3、给对象类型的属性注入值

4、给list集合注入值

​5、给map集合注入值

6、通过工厂方式给date类型属性注入值

​7、使用util命名空间配置单独的list集合,

8、使用p命名空间简化依赖注入的配置

9、前文使用到的帮助类


1、给普通字段注入值

	<!-- 为对象的每个属性分别赋值 -->
	<bean id="s1" class="com.zl.pojo.Student">
		<property name="name" value="张三"></property>
		<property name="age" value="20"></property>
		<property name="sex" value="男"></property>
	</bean>
	
	<!-- 如果属性的值带有特殊字符,需要转义<![CDATA[<张三>]]> -->
	<bean id="s2" class="com.zl.pojo.Student">
		<property name="name">
			<value><![CDATA[<张三>]]></value>
		</property>
		<property name="age" value="20"></property>
		<property name="sex" value="男"></property>
	</bean>	

2、通过构造方法给参数赋值以及调用指定的构造方法 

指明index和type的值,指定相应的构造方法

	<bean id="s3" class="wendi.entity.Student">
		<!-- 代表一个构造方法方法参数 constructor-arg:他的顺序并不代表参数的顺序 可以通过index执行该标签的值赋值给那个参数 -->
		<constructor-arg value="20" index="0" type="int"></constructor-arg>
		<constructor-arg value="男" index="1" type="java.lang.String"></constructor-arg>
	</bean>

	<bean id="s4" class="wendi.entity.Student">
		<!-- 代表一个构造方法方法参数 constructor-arg:他的顺序并不代表参数的顺序 可以通过index执行该标签的值赋值给那个参数 -->
		<constructor-arg value="20" index="0" type="double"></constructor-arg>
		<constructor-arg value="张三" index="1" type="java.lang.String"></constructor-arg>
	</bean>

3、给对象类型的属性注入值

	<bean id="s1" class="wendi.entity.Student">
		<property name="name" value="张三"></property>
		<property name="age" value="20"></property>
	</bean>


	<!-- 当对象的属性也同时为一个对象时 ,使用ref引用已经存在的实例,其中s1为已经存在的实例 -->
	<bean id="t1" class="wendi.entity.Teacher">
		<property name="name" value="李老师"></property>
		<property name="student" ref="s1"></property>
	</bean>
	<!-- 也可以直接在property的标签内部再写一个bean标签定义一个新的实例 -->
	<bean id="t2" class="wendi.entity.Teacher">
		<property name="name" value="李老师"></property>
		<property name="student">
			<bean class="wendi.entity.Student">
				<property name="name" value="张四"></property>
				<property name="age" value="20"></property>
			</bean>
		</property>
	</bean>

 

4、给list集合注入值

	<bean id="s1" class="wendi.entity.Student">
		<property name="name" value="张三"></property>
		<property name="age" value="20"></property>
	</bean>

	<bean id="s2" class="wendi.entity.Student">
		<property name="name">
			<value><![CDATA[<张三>]]></value>
		</property>
		<property name="age" value="20"></property>
		<property name="sex" value="男"></property>
	</bean>

	<bean id="t3" class="wendi.entity.Teacher">
		<property name="name" value="李老师"></property>
		<property name="students">
			<list>
				<!-- 引用已经配置好的bean -->
				<ref bean="s1"></ref>
				<ref bean="s2"></ref>
				<!-- 内部bean -->
				<bean class="wendi.entity.Student">
					<property name="name" value="张三"></property>
					<property name="age" value="80"></property>
				</bean>
			</list>
		</property>
	</bean>

5、给map集合注入值

	<bean id="s1" class="wendi.entity.Student">
		<property name="name" value="张三"></property>
		<property name="age" value="20"></property>
	</bean>

	<bean id="s2" class="wendi.entity.Student">
		<property name="name">
			<value><![CDATA[<张三>]]></value>
		</property>
		<property name="age" value="20"></property>
		<property name="sex" value="男"></property>
	</bean>

	<bean id="t4" class="wendi.entity.Teacher">
		<property name="name" value="李老师"></property>
		<property name="maps">
			<!-- 利用map标签添加一个个键值对 -->
			<map>
				<entry key="stu1" value-ref="s1"></entry>
				<entry key="stu2" value-ref="s2"></entry>
			</map>
		</property>
	</bean>

6、通过工厂方式给date类型属性注入值

	<!-- 配置当前系统时间 -->
	<bean id="nowDate" class="java.util.Date"></bean>
	<!-- 2018-06-22 -->
	<bean id="s5" class="wendi.entity.Student">
		<property name="name" value="张三"></property>
		<property name="age" value="20"></property>
		<property name="money" value="100"></property>
		<property name="sex" value="男"></property>
		<property name="bir" ref="nowDate"></property>
	</bean>

	<!-- 配置日期格式化工厂:java.text.SimpleDateFormat -->
	<!-- 定义固定时间格式 -->
	<bean id="dateFactory" class="java.text.SimpleDateFormat">
		<constructor-arg value="yyyy年MM月dd日"></constructor-arg>
	</bean>

	<bean id="s5" class="wendi.entity.Student">
		<property name="name" value="张三"></property>
		<property name="age" value="20"></property>
		<property name="sex" value="男"></property>
		<property name="bir">
			<bean factory-bean="dateFactory" factory-method="parse">
				<constructor-arg value="2008年08月08日"></constructor-arg>
			</bean>
		</property>
	</bean>

7、使用util命名空间配置单独的list集合,

使用之前需要导入util命名空间包

xmlns:util="http://www.springframework.org/schema/util"
	<bean id="s1" class="wendi.entity.Student">
		<property name="name" value="张三"></property>
		<property name="age" value="20"></property>
		<property name="sex" value="男"></property>
	</bean>
	<bean id="s2" class="wendi.entity.Student">
		<property name="name">
			<value><![CDATA[<张三>]]></value>
		</property>
		<property name="age" value="20"></property>
		<property name="sex" value="男"></property>
	</bean>

	<bean id="t5" class="wendi.entity.Teacher">
		<property name="name" value="李老师"></property>
		<property name="students" ref="stus"></property>
	</bean>

	<!-- 通过util命名空间配置一个list集合 -->
	<util:list id="stus">
		<ref bean="s1"></ref>
		<ref bean="s2"></ref>
		<bean class="wendi.entity.Student">
			<property name="name" value="张大帅"></property>
		</bean>
	</util:list>

8、使用p命名空间简化依赖注入的配置

xmlns:p="http://www.springframework.org/schema/p"
	<bean id="s6" class="wendi.entity.Student" 
		p:name="王五" 
		p:age="30"
		p:bir-ref="nowDate">
	</bean>

	<bean id="nowDate" class="java.util.Date"></bean>

9、前文使用到的帮助类

package wendi.entity;

import java.util.Date;

public class Student {
	private String name;
	private int age;
	private double money;
	private String sex;
	private Date bir;

	public Student() {

	}

	public Student(int age, String sex) {
		System.out.println("int在前面");
		this.age = age;
		this.sex = sex;
	}

	public Student(double money, String name) {
		System.out.println("double在前面");
		this.money = money;
		this.name = name;
	}

	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;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public double getMoney() {
		return money;
	}

	public void setMoney(double money) {
		this.money = money;
	}

	public Date getBir() {
		return bir;
	}

	public void setBir(Date bir) {
		this.bir = bir;
	}

	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", money=" + money + ", sex=" + sex + ", bir=" + bir + "]";
	}
}


--------------我是帮助类的分割线------------------


package wendi.entity;

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

public class Teacher {
	private String name;
	private Student student;
	private List<Student> students;
	private Map<String, Student> maps;

	public String getName() {
		return name;
	}

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

	public Student getStudent() {
		return student;
	}

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

	public List<Student> getStudents() {
		return students;
	}

	public void setStudents(List<Student> students) {
		this.students = students;
	}

	public Map<String, Student> getMaps() {
		return maps;
	}

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

	@Override
	public String toString() {
		return "Teacher [name=" + name + ", student=" + student + ", students=" + students + ", maps=" + maps + "]";
	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值