1. 程式人生 > >spring-自動裝配,繼承,抽象,依賴注意點-複習-4

spring-自動裝配,繼承,抽象,依賴注意點-複習-4

這一次得出的結論是depend-on和autowire結合使用,autowire在指定了parent後可能不會再次裝配。

其次是abstract的資料模板功能,同時不能被容器例項化。 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/util
                           http://www.springframework.org/schema/util/spring-util.xsd">
	<!-- 使用abstract來指定bean為抽象bean,作為資料模板,同時也不能被容器例項化 -->
	<bean id="order" class="com.springstudy.vo.Order" abstract="true">
	</bean>
	<!-- 通過parent指定繼承某個bean,然後重新設定屬性覆蓋父屬性,可以繼承非抽象bean,但不能得到父節點的autowire和abstract屬性 -->
	<bean id="order1" parent="order">
		<property name="id" value="1"></property>
	</bean>
	<bean id="order2" parent="order">
		<property name="id" value="2"></property>
	</bean>
	<!-- 藉助util名稱空間,配置一個公用的list集合 ,util中還提供了map和set等 -->
	<util:list id="lstOrds">
		<bean class="com.springstudy.vo.Order">
			<property name="id" value="10"></property>
		</bean>
		<bean class="com.springstudy.vo.Order">
			<property name="id" value="11"></property>
		</bean>
		<bean class="com.springstudy.vo.Order">
			<property name="id" value="14"></property>
		</bean>
		<ref bean="order1" />
	</util:list>
	<!--自動裝配的主要方式是byName和byType,但從本源上來說,如果一個也沒匹配上,那麼就報告異常,如下采用byName方式, 而且確定穩定的行為可能更實際,所以自動裝配更適合小範圍使用,畢竟穩定壓倒一切捷徑方式,所以這個屬性專案不採用。 
		而且在使用了parent後,如果父bean已經配置了相應的屬性,那麼autowire毫無作用,如果父bean沒有設定相應屬性,那麼autowire才能 
		生效 -->
	<bean id="customer2" parent="customer" autowire="byName">
	</bean>
	<!-- depends-on指定依賴某個bean,往往和自動裝配結合使用,如果沒有配置相應的bean則報告異常 -->
	<bean id="customer3" class="com.springstudy.vo.Customer"
		autowire="byName" depends-on="lstOrds">
	</bean>
	<!-- 展示了list,map,properties配置方式 -->
	<bean id="customer" class="com.springstudy.vo.Customer">
		<property name="lstOrds">
			<list>
				<ref bean="order1" />
				<bean class="com.springstudy.vo.Order">
					<property name="id" value="3"></property>
				</bean>
			</list>
		</property>
		<property name="mapOrds">
			<map>
				<entry key="100000971956" value-ref="order1"></entry>
				<entry key="2912105">
					<ref bean="order2" />
				</entry>
				<entry key="291210599">
					<bean class="com.springstudy.vo.Order">
						<property name="id" value="3"></property>
					</bean>
				</entry>
			</map>
		</property>
		<property name="props">
			<props>
				<prop key="Cat10598">485118-546548-2198765-879845</prop>
				<prop key="Cat10508">895118-556548-2198765-109845</prop>
			</props>
		</property>
	</bean>

</beans>
package com.springstudy.test;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.springstudy.vo.Customer;

class CustomerTest {
	static ApplicationContext ctx; 
	Customer springCustomer=ctx.getBean("customer", Customer.class);
	Customer springCustomer2=ctx.getBean("customer2", Customer.class);
	Customer springCustomer3=ctx.getBean("customer3", Customer.class);
	@BeforeAll
	static void setUpBeforeClass() throws Exception {
		ctx = new ClassPathXmlApplicationContext("PCBU01.xml");
	}

	@Test
	void test() {
		//springCustomer.getLstOrds().forEach(x->System.out.println(x.getId()));
		springCustomer.getMapOrds().forEach((x,y)->System.out.println(x+"->"+y.getId()));
		springCustomer.getProps().forEach((x,y)->System.out.println(x+"->"+y));
		
		springCustomer2.getLstOrds().forEach(x->System.out.println(x.getId()));
		
		springCustomer3.getLstOrds().forEach(x->System.out.println(x.getId()));
	}

}
100000971956->1
2912105->2
291210599->3
Cat10598->485118-546548-2198765-879845
Cat10508->895118-556548-2198765-109845
1
3
10
11
14
1