Bean自动装配的5种模式

一共有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>

猜你喜欢

转载自blog.csdn.net/u010142437/article/details/80884972