Spring的核心容器Bean的基础知识(二)

这篇来介绍一下Bean基础中的依赖相关。Bean元素的depends-on属性,depends-on哪个bean。

(一)

Java Bean 之间的依赖关系:可以通过<ref/>元素来手动指定Bean的依赖关系。即同ref表示指定依赖。

Bean的依赖关系可以改变Bean的实例化顺序,被依赖的Bean要先于依赖的Bean优先被实例化。

<ref/>的三种模式和用法:

local:当前同一个xml,只能用相同的id表示。

bean:不一定是在同一个xml中,可以用相同的id或name表示。

parent:允许引用当前的BeanFactory或ApplicationContext的父BeanFactory或ApplicationContext中的Bean。

 

HelloWorld.java(定义Bean)

 

package com.gc.acion;

import java.util.Date;

public class HelloWorld {
	
	private String msg = null;
	private Date date = null;
	
	public void init(){
		this.msg = "wangyj";
	}
	
	public void cleanup(){
		this.msg = "";
		System.out.println("HelloWorld中的"+this.msg+"已销毁");
	}

	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配置:

 

 

<bean id="HelloWorld" class="com.gc.acion.HelloWorld" scope="singleton" init-method="init" destroy-method="cleanup" depends-on="date">
		<!-- local属性模式 -->
		<property name="date">
			<ref local="date"/>
		</property>
		<!-- bean属性模式 -->
		<property name="date">
			<ref bean="date"/>
		</property>
		<!-- parent属性模式 -->
		<property name="date">
			<ref parent="date"/>
		</property>
	</bean><!-- local属性模式 -->
		<property name="date">
			<ref local="date"/>
		</property>
		<!-- bean属性模式 -->
		<property name="date">
			<ref bean="date"/>
		</property>
		<!-- parent属性模式 -->
		<property name="date">
			<ref parent="date"/>
		</property>
	</bean>

 

 

(二)在第(一)节中说的是指定依赖,即明确指出一个Bean依赖另一个Bean。

现在来说明另一种依赖方式。Bean的自动装配。Bean的依赖关系根据xml配置自动关联依赖,不需要程序员自己手动去指定。

Bean的自动装配有5种模式:autowrtite=""

byName:Bean.java中的属性根据相同的名字去找xml配置中找id相同的bean。

 

<bean id="HelloWorld" class=""com.gc.acion.HelloWorld" scope="singleton" autowire="byName">
		<property name="msg">
			<value>wangyj</value>
		</property>
	</bean>
	
	<bean id="date" class="java.Util.Date"></bean>

 

 

 

byType:Bean.java中的属性根据相同的属性的类型去xml配置中找相同类型的id。比如都是Date

 

<bean id="HelloWorld" class=""com.gc.acion.HelloWorld" scope="singleton" autowire="byType">
		<property name="msg">
			<value>wangyj</value>
		</property>
	</bean>
	
	<bean id="date" class="java.Util.Date"></bean>

 

 

 

constructor:Bean.java中构造函数中的参数,去找合适类型的Bean。

 

<bean id="HelloWorld" class=""com.gc.acion.HelloWorld" scope="singleton" autowire="constructor">
		<property name="msg">
			<value>wangyj</value>
		</property>
	</bean>
	
	<bean id="date" class="java.Util.Date"></bean>

 

 

 

autodetect:优先构造方法constructor,然后byType

 

	<bean id="HelloWorld" class=""com.gc.acion.HelloWorld" scope="singleton" autowire="autodetect">
		<property name="msg">
			<value>wangyj</value>
		</property>
	</bean>
	
	<bean id="date" class="java.Util.Date"></bean>

 

 

 

no:需要指定依赖<ref/>

 

	<bean id="HelloWorld" class=""com.gc.acion.HelloWorld" scope="singleton" autowire="no">
		<property name="msg">
			<value>wangyj</value>
		</property>
		<property name="date">
			<ref local="date"/>
		</property>
	</bean>
	
	<bean id="date" class="java.Util.Date"></bean>


(三)以上是自动装配,但是自动装配是否能保证bean的每个属性时候都设定完成,所以我们需要加一个依赖检查。

 

一般自动装配+依赖检查配合使用,确保bean的属性被设定完成,否则会报错提示。

依赖检查关键字:dependency-check

有四种模式:

simple:只针对基本类型、String、集合

 

<bean id="HelloWorld" class=""com.gc.acion.HelloWorld" scope="singleton" autowire="autodetect" dependency-check="simple">
	</bean>
	
	<bean id="date" class="java.Util.Date"></bean>
检查msg属性

 

 

 

object:依赖的对象

 

<bean id="HelloWorld" class=""com.gc.acion.HelloWorld" scope="singleton" autowire="autodetect" dependency-check="objects"></bean>
	
	<bean id="date" class="java.Util.Date"></bean>
检查date属性

 

 

 

all:全部

 

<bean id="HelloWorld" class=""com.gc.acion.HelloWorld" scope="singleton" autowire="autodetect" dependency-check="all"></bean>
	
	<bean id="date" class="java.Util.Date"></bean>
即检查msg也检查date

 

none:不检查(默认)

 

当bean属性都有默认值或者不需要检查bean的属性是否都需要设置到bean上时,依赖检查的作用就不是很大了。

 

本章讲的是Bean的注入方式,下一章讲集合的注入方式。


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值