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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值