Bean自动装配的5种模式

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

一共有5种模式:byName、byType、constructor、autodetect和no

1、byName模式

就是通过Bean的属性名字进行自动装配,在配置文档中查找一个与将要装配的属性同样名字的Bean。示例如下:

HelloWorld。

package com.example.demo.test;

import java.util.Date;

public class HelloWorld {

	private String msg;

	private Date date;

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

}

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE  beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
	<bean id="HelloWorld" class="com.example.demo.test.HelloWorld"
		autowire="byName">
		<property name="msg">
			<value>HelloWorld</value>
		</property>
	</bean>
	<bean id="date" class="java.util.Date"></bean>
</beans>

2、使用byType模式

指的就是如果XML中正好有一个与属性类型一样的Bean,就自动装配这个属性。如果存在多个这样的Bean,就抛出一个异常。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE  beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
	<bean id="HelloWorld" class="com.example.demo.test.HelloWorld"
		autowire="byType">
		<property name="msg">
			<value>HelloWorld</value>
		</property>
	</bean>
	<bean id="date" class="java.util.Date"></bean>
</beans>

如果没有匹配成功,则什么都不会发生,属性不会被设置。如果这不是开发人员想要的情况,则可以通过设置 dependency-check="objects"来指定应该抛出异常。

3、使用constructor模式

就是根据构造函数的参数来自动装配,示例如下:

package com.example.demo.test;

import java.util.Date;

public class HelloWorld {

	private String msg;

	private Date date;

	public HelloWorld(Date date) {
		this.date = date;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE  beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
	<bean id="HelloWorld" class="com.example.demo.test.HelloWorld"
		autowire="constructor">
		<property name="msg">
			<value>HelloWorld</value>
		</property>
	</bean>
	<bean id="date" class="java.util.Date"></bean>
</beans>

4、使用autodetect模式

就是通过对Bean检查类的内部来选择constructor或者byType,优先constructor,示例如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE  beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
	<bean id="HelloWorld" class="com.example.demo.test.HelloWorld"
		autowire="autodetect">
		<property name="msg">
			<value>HelloWorld</value>
		</property>
	</bean>
	<bean id="date" class="java.util.Date"></bean>
</beans>

5、使用no模式

就是不使用自动装配,在很多企业不鼓励使用自动装配,因为它对应Bean之间的参考依赖关系不清晰,示例如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE  beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
	<bean id="HelloWorld" class="com.example.demo.test.HelloWorld"
		autowire="no">
		<property name="msg">
			<value>HelloWorld</value>
		</property>
		<property name="date">
			<ref bean="date"/>
		</property>
	</bean>
	<bean id="date" class="java.util.Date"></bean>
</beans>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

u010142437

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值