1. 程式人生 > >Spring4之Bean之間的關係(繼承、依賴、引用)Bean的作用範圍

Spring4之Bean之間的關係(繼承、依賴、引用)Bean的作用範圍

假如說員工Employee的屬性中,政治面貌大都是團員,民族大都是漢族

可以配置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="abstractEmployee" class="com.zhiqi.model.Employee" abstract="true">
		<property name="zzmm" value="團員"></property>
		<property name="nation" value="漢族"></property>
	</bean>

	<bean id="employee1" parent="abstractEmployee">
		<property name="id" value="10080"></property>
		<property name="name" value="老王"></property>
		<property name="sex" value="男"></property>
	</bean>
	<bean id="employee2" parent="abstractEmployee">
		<property name="id" value="10090"></property>
		<property name="name" value="小麗"></property>
		<property name="sex" value="女"></property>
	</bean>
	
</beans>

實體類:
package com.zhiqi.model;

public class Employee {

	private int id;
	private String name;
	private String sex;
	private Car car;
	private String zzmm;
	private String nation;

	public Employee() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Employee(Car car) {
		super();
		this.car = car;
	}

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Car getCar() {
		return car;
	}
	public void setCar(Car car) {
		this.car = car;
	}
	public String getZzmm() {
		return zzmm;
	}
	public void setZzmm(String zzmm) {
		this.zzmm = zzmm;
	}
	public String getNation() {
		return nation;
	}
	public void setNation(String nation) {
		this.nation = nation;
	}

	@Override
	public String toString() {
		return "Employee [id=" + id + ", name=" + name + ", sex=" + sex + ", car=" + car + ", zzmm=" + zzmm
				+ ", nation=" + nation + "]";
	}
	
}

測試:


這時如果有人是黨員,可以覆寫政治面貌

<bean id="employee3" parent="abstractEmployee">
		<property name="id" value="10091"></property>
		<property name="name" value="老習"></property>
		<property name="sex" value="男"></property>
		<property name="zzmm" value="黨員"></property>
	</bean>

依賴:場景描述:員工所在單位有一個研發試驗室,保密級別高,需要有通行證才能進入


這個時候可以配置依賴的bean

員工來了是構造方法裡的模擬

	public Employee() {
		super();
		// TODO Auto-generated constructor stub
		System.out.println("員工來了");
	}

獲取通行證是模擬
public class PassCheck {
	public PassCheck(){
		System.out.println("獲取研發實驗室通行證");
		//...業務省略
	}
}

執行:


引用bean:<property name="car" ref="car"></property>

最基本的關係不多敘述

總結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="abstractEmployee" class="com.zhiqi.model.Employee" abstract="true">
		<property name="zzmm" value="團員"></property>
		<property name="nation" value="漢族"></property>
	</bean>

	<bean id="employee1" parent="abstractEmployee" depends-on="passCheck">
		<property name="id" value="10080"></property>
		<property name="name" value="老王"></property>
		<property name="sex" value="男"></property>
		<!-- 引用關係 -->
		<!--property name="car" ref="car"></property-->
	</bean>
	
	<!-- 依賴關係 -->
	<!-- 獲取通行證的bean -->
	<bean id="passCheck" class="com.zhiqi.service.PassCheck"></bean>
	
</beans>

【bean的作用範圍】