4.Spring DI

1.依赖注入示例

(1)Teacher.java

package nuc.hzb.entity;

public class Teacher {
	
	private String name;
	private int 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;
	}

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

(2)Course.java

package nuc.hzb.entity;

public class Course {
	private String courseName;
	private int courseHour;
	// 授课老师依赖于Teacher类
	private 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;
	}

	@Override
	public String toString() {
		return "Course [courseName=" + courseName + ", courseHour=" + courseHour + ", teacher=" + teacher + "]";
	}
	
}

(3)applicationContext.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">

	<bean id="teacher" class="nuc.hzb.entity.Teacher">
		<property name="name" value="hzb"></property>
		<property name="age" value="18"></property>
	</bean>
	
	<bean id="course" class="nuc.hzb.entity.Course">
		<property name="courseName" value="java"></property>
		<property name="courseHour" value="54"></property>
		<property name="teacher" ref="teacher"></property>
	</bean>

</beans>

(4)Test.java

package nuc.hzb.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		Object course = context.getBean("course");
		System.out.println(course);
	}
}

2.IOC容器赋值

(1)如果是简单类型

(8个基本 + String)

使用value属性

(2)如果是对象类型

ref=“需要引用的id值”,因此实现了对象与对象之间的依赖关系

(3)获取

ApplicationContext context = new ClassPathXmlApplicationContext(“applicationContext.xml”);
context.getBean(需要获取的bean的id值)

3.依赖注入3种方式

(1)set注入

通过setXxx()赋值
赋值,默认使用的是setXxx();
依赖注入底层是通过反射实现的。

	<bean id="teacher" class="nuc.hzb.entity.Teacher">
        <property name="name" value="hzb"></property> 
		<property name="age" value="18"></property> 
	</bean>
	
	<bean id="course" class="nuc.hzb.entity.Course">
		<property name="courseName" value="java"></property> -->
		<property name="courseHour" value="54"></property> -->
		<property name="teacher" ref="teacher"></property> -->
	</bean>

(2)构造器注入

通过构造方法赋值

	<bean id="teacher" class="nuc.hzb.entity.Teacher">
<!--  		顺序严格一致,不一致使用index指定位置,从0开始;或指定参数名;或指定类型-->	
<!-- 		<constructor-arg value="24" index="1"></constructor-arg> -->
<!-- 		<constructor-arg value="hzb" index="0"></constructor-arg> -->
		<constructor-arg value="24" type="int"></constructor-arg>
		<constructor-arg value="hzb" type="String"></constructor-arg>
	</bean>
	
	<bean id="course" class="nuc.hzb.entity.Course">
		<constructor-arg value="100" name="courseHour"></constructor-arg>
		<constructor-arg value="c" name="courseName"></constructor-arg>
		<constructor-arg ref="teacher" name="teacher"></constructor-arg>
	</bean>

需要注意:如果 的顺序 与构造方法参数的顺序不一致,则需要通过type或者index或name指定

建议:写name来指定,见名知意

(3)p命名空间注入

①引入p命名空间

xmlns:p=“http://www.springframework.org/schema/p”

<?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="nuc.hzb.entity.Teacher" p:age="18" p:name="hzb">
	</bean>
	
	<bean id="course" class="nuc.hzb.entity.Course" p:courseName="大数据" p:courseHour="120" p:teacher-ref="teacher">
	</bean>

</beans>

②简单类型

(8个基本 + String)

p:属性名=“属性值”

③引用类型

p:属性名-ref=“引用的id”
注意:多个p赋值的时候要有空格

无论是String还是Int/short/long等类型,在赋值时都是 value=“具体的值” ,
因此建议此种情况需要配合 name\type进行区分

4.注入各种集合数据类型

(1)类型

String[]、List、Set、Map、Properties

(2)示例

①applicationContext.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"
	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="collectionDemo" class="nuc.hzb.entity.AllCollectionType">
	
		<property name="array">
			<array>
				<value>array1</value>
				<value>array2</value>
				<value>array3</value>
			</array>
			
		</property>
		
		<property name="list">
			<list>
				<value>list1</value>
				<value>list2</value>
				<value>list3</value>
			</list>
		</property>
		
		<property name="set">
			<set>
				<value>set1</value>
				<value>set2</value>
				<value>set3</value>
			</set>
		</property>
		
		<property name="map">
			<map>
				<entry>
					<key>
						<value>mapKey1</value>
					</key>
					<value>mapValue1</value>
				</entry>
				
				<entry>
					<key>
						<value>mapKey2</value>
					</key>
					<value>mapValue2</value>
				</entry>
			</map>
		</property>
		
		<property name="properties">
			<props>
				<prop key="propertiesKey1">propertiesValue1</prop>
				<prop key="propertiesKey2">propertiesValue2</prop>
			</props>
		</property>
	</bean>

</beans>

②Test.java
package nuc.hzb.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import nuc.hzb.entity.AllCollectionType;

public class Test {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		AllCollectionType collectionDemoBean = (AllCollectionType)context.getBean("collectionDemo");
		System.out.println(collectionDemoBean);
	}
}

(3)注意

set、list、数组各自都有自己的标签 ,但是也可以混着用

但是一般建议对号入座

5.通过构造方法赋值

(1)AllCollectionType.java

// 多余代码省略
public class AllCollectionType {
	private List<String> list;
    // 必须有这两个构造器
	public AllCollectionType() {}

	public AllCollectionType(List<String> list) {
		super();
		this.list = list;
	}	
}

(2)applicationContext.xml

<bean id="collectionDemo1" class="nuc.hzb.entity.AllCollectionType">
	<constructor-arg name="list">
		<list>
			<value>list1</value>
			<value>list2</value>
			<value>list3</value>
		</list>
	</constructor-arg>
</bean>

(3)注意

在IOC中定义bean的前提:该bean的类必须提供了无参构造,以及有参构造

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值