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>