1. 程式人生 > >Spring——第二章 Spring與IoC(三)

Spring——第二章 Spring與IoC(三)

cirl + shift + F 格式化程式碼

2.3.6 使用內部Bean注入

	<bean id="myStudent" class="com.bjpowernode.di10.Student">
		<property name="name" value="張三"/>
		<property name="age" value="23"/>
		<property name="school">
			<bean class="com.bjpowernode.di10.School">
				<property name="name" value="清華大學"/>
			</bean>
		</property>
	</bean>

2.3.7 同類抽象Bean

  若子Bean有需要公共部分,可以繼承這個Bean。

<?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 -->   
	<bean id="baseStudent" class="com.bjpowernode.di11.Student" abstract="true">
		<property name="school" value="清華大學"/>
		<property name="department" value="計算機系"/>
	</bean>

	<bean id="myStudent" parent="baseStudent">
		<property name="name" value="張三"/>
		<property name="age" value="23"/>
		<property name="school" value="清華大學"/>
		<property name="department" value="計算機系"/>
	</bean>
	<bean id="myStudent2" parent="baseStudent">
		<property name="name" value="李四"/>
		<property name="age" value="23"/>
		<property name="school" value="清華大學"/>
		<property name="department" value="計算機系"/>
	</bean>
	<bean id="myStudent3" parent="baseStudent">
		<property name="name" value="王五"/>
		<property name="age" value="23"/>
		<property name="school" value="清華大學"/>
		<property name="department" value="計算機系"/>
	</bean>
	
</beans>

2.3.8 異類抽象Bean

<?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="baseBean" abstract="true">
    	<property name="school" value="清華大學"/>
		<property name="department" value="計算機系"/>
    </bean>
    
    <bean id="myTeacher" class="com.bjpowernode.di12.Teacher" parent="baseBean">
		<property name="name" value="大力"/>
		<property name="workAge" value="43"/>
	</bean>
    
    
	<bean id="myStudent" class="com.bjpowernode.di12.Student" parent="baseBean">
		<property name="name" value="張三"/>
		<property name="age" value="23"/>
	</bean>
	
	
	
</beans>

2.3.9 為應用指定多個Spring配置檔案

包含關係和平等關係

按照模組來分。

一個檔案資源、多個檔案資源、一個帶萬用字元的資源

平等關係:

package com.bjpowernode.di13;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
	@Test
	public void Test01(){
		/*
		String resource = "com/bjpowernode/di13/spring-base.xml";
		String resource2 = "com/bjpowernode/di13/spring-beans.xml";
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(resource,resource2);
		*/
		/*
		String resource = "com/bjpowernode/di13/spring-base.xml";
		String resource2 = "com/bjpowernode/di13/spring-beans.xml";
		String[] resources = {resource,resource2};
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(resources);
		*/
		String resource = "com/bjpowernode/di13/spring-*.xml";
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(resource);
		Student student = (Student) applicationContext.getBean("myStudent");
		System.out.println(student);
		
		Teacher teacher = (Teacher) applicationContext.getBean("myTeacher");
		System.out.println(teacher);
		
	}
}

包含關係:

<?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">
        <!--該xml檔案包含兩個子類xml檔案-->
	<import resource="classpath:com/bjpowernode/di14/spring-base.xml"/>
	<import resource="classpath:com/bjpowernode/di14/spring-beans.xml"/>

</beans>