Spring IOC (DI) 依赖注入的四种方式

依赖注入的四种方式:
set 注入

赋值,默认使用的是set() 方法,依赖注入底层是通过反射实现的

<bean id="student" class="cust.zaw.entity.Student">
	<property name="stuName" value="lc"></property>
</bean>
构造器注入

使用构造方法注入

<bean id="student" class="cust.zaw.entity.Student">

参数顺序相同的话,可以不写index
<constructor-arg value=""></constructor-arg>

参数顺序不同的话,写index或者name或者type

<constructor-arg value="" index=“0”></constructor-arg>
<constructor-arg value="" index=“1”></constructor-arg>

<constructor-arg value="" name=“”></constructor-arg>

<constructor-arg value="" type=“”></constructor-arg>

</bean>
p命名空间注入

引入命名空间
就是加一句话:xmlns:p=“http://www.springframework.org/schema/p
在这里插入图片描述
也可以这样引入,它会自动引入命名空间
在这里插入图片描述
引入命名空间后,就可以这样以这样的方式实现依赖注入

<bean id="student" class="cust.zaw.entity.Student" p:stuAge="" p:stuName="" p:stuNO-ref="">
</bean>
  • 注意:
    无论是String 还是int / short / long ,在赋值时都是value=“值”,
    因此建议 配合type / name 进行区分
  • IOC 容器赋值:
    简单类型,value=“”
    对象类型(当一个类中有其他类 类型,为其赋值时使用),ref=“需要引用的id值”
注入各种集合数据类型

List Set Map properties

  • 写一个实体类
package cust.zaw.entity;

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

public class AllCollectionType {
	private List<String> list;
	private String[] array;
	private Set<String> set;
	private Map<String,String> map;
	private Properties props;
	public List<String> getList() {
		return list;
	}
	public void setList(List<String> list) {
		this.list = list;
	}
	public String[] getArray() {
		return array;
	}
	public void setArray(String[] array) {
		this.array = array;
	}
	public Set<String> getSet() {
		return set;
	}
	public void setSet(Set<String> set) {
		this.set = set;
	}
	public Map<String, String> getMap() {
		return map;
	}
	public void setMap(Map<String, String> map) {
		this.map = map;
	}
	public Properties getProps() {
		return props;
	}
	public void setProps(Properties props) {
		this.props = props;
	}
	@Override
	public String toString() {
		String strContent=null;
		for(String str : array) {
			strContent +=str + ",";
		}
		return "AllCollectionType [getList()=" + getList() + ", getArray()=" + Arrays.toString(getArray())
				+ ", getSet()=" + getSet() + ", getMap()=" + getMap() + ", getProps()=" + getProps() + "]"+strContent;
	}
	
}

  • 编写配置文件
<bean id="collectionDemo" class="cust.zaw.entity.AllCollectionType">
		<property name="list">
			<list>
				<value>足球</value>
				<value>篮球</value>
				<value>乒乓球</value>
			</list>
		</property>
		<property name="array">
			<array>
				<value>足球1</value>
				<value>篮球1</value>
				<value>乒乓球1</value>
			</array>
		</property>
		<property name="set">
			<set>
				<value>足球2</value>
				<value>篮球2</value>
				<value>乒乓球2</value>
			</set>
		</property>
		<property name="map">
			<map>
				<entry>
					<key>
						<value>football3</value>
					</key>
					<value>足球3</value>
				</entry>
				<entry>
					<key>
						<value>basketball3</value>
					</key>
					<value>篮球3</value>
				</entry>
				<entry>
					<key>
						<value>ppq3</value>
					</key>
					<value>乒乓球3</value>
				</entry>
			</map>
		</property>
		<property name="props">
			<props>
				<prop key="football4">足球4</prop>
				<prop key="baskball4">篮球4</prop>
				<prop key="pp4">乒乓球4</prop>
			</props>
		</property>		
	</bean>
  • 获取输出
public static void collectionDemp() {
		ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
		AllCollectionType type=(AllCollectionType)context.getBean("collectionDemo");
		System.out.println(type);
	}
  • 给对象类型赋值null:
<!-- 注意没有<value> -->

<property name="name">
			<null/>
</property>
  • 赋空值
<property name="name">
			<value></value>
</property>
自动装配注入(只适用于 ref 类型):

约定优于配置

  • byName:自动寻找:其他bean的id值 = 该Course类的属性名
  • byType:其他类型(class)是否与该Course类的ref 属性类型一致
    (必须满足当前ioc容器只有一个bean满足)
  • constructor:其他bean的类型(class)是否与该Course 类的构造方法参数 的类型一致;
<!--
		autowire="byName"
		Course类中有一个ref属性teacher(属性名),并且该ioc容器中恰好有一个 bean的id也是teacher
		bean的id=类的属性名 
 -->
<bean id="student" class="cust.zaw.entity.Student" autowire="byName">
		<property name="stuNO" value="2"></property>
		<property name="stuName" value="lc"></property>
		<property name="stuAge" value="24"></property>
		<!-- <property name="teacher" ref="teacher"></property>-->
</bean>
  • 可以在头文件中,一次性将ioc容器的所有bean 统一设置成自动装配
    自动装配虽然可以减少代码量,但是会降低程序可读性
<beans xmlns="http://www.springframework.org/schema/beans"
	....
	default-autowire="byName">
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值