Spring的分模块开发的配置

分模块开发配置

一、在加载配置文件的时候,加载多个

准备两个配置文件分别为
applicationContext.xml
Spring的分模块开发的配置_第1张图片
applicationContext2.xml
Spring的分模块开发的配置_第2张图片

package com.itzheng.spring.demo5;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/*
 * 复杂类型的属性注入
 */
public class SpringDemo5 {
	@Test
	public void demo1() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml","applicationContext2.xml");
		CollectionBean collectionBean = (CollectionBean) applicationContext.getBean("collectionBean");
		System.out.println(collectionBean);
	}
}

二、在一个配置文件当中引入多个配置文件

import
Spring的分模块开发的配置_第3张图片

package com.itzheng.spring.demo5;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/*
 * 复杂类型的属性注入
 */
public class SpringDemo5 {
	@Test
	public void demo1() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		CollectionBean collectionBean = (CollectionBean) applicationContext.getBean("collectionBean");
		System.out.println(collectionBean);
	}
}

三、完成测试代码

package com.itzheng.spring.demo5;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
/*
 * 集合属性的注入:
 */
public class CollectionBean {
	private String[] arrs;
	private List<String> list;
	private Set<String> set;
	private Map<String, String> map;
	public void setArrs(String[] arrs) {
		this.arrs = arrs;
	}
	public void setList(List<String> list) {
		this.list = list;
	}
	public void setSet(Set<String> set) {
		this.set = set;
	}
	public void setMap(Map<String, String> map) {
		this.map = map;
	}
	@Override
	public String toString() {
		return "CollectionBean [arrs=" + Arrays.toString(arrs) + ", list=" + list + ", set=" + set + ", map=" + map
				+ "]";
	}
}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p"
	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">
	<!-- Spring的属性注入的方式 -->
	<!-- 构造方法的方式 -->
	<bean id="car" class="com.itzheng.spring.demo4.Car">
		<constructor-arg name="name" value="宝马" />
		<constructor-arg name="price" value="800000" />
	</bean>
	<bean id="boos" class="com.itzheng.spring.demo4.Boos">
		<constructor-arg name="name" value="李四" />
		<constructor-arg name="car" ref="car" />
	</bean>
	<!-- set方法方式符注入 -->
	<!-- <bean id="car2" class="com.itzheng.spring.demo4.Car2"> <property name="name" 
		value="奔驰"></property> <property name="price" value="1000000"></property> 
		</bean> -->
	<!-- SpEL的属性注入 -->
	<bean id="carInfo" class="com.itzheng.spring.demo4.CarInfo">
	</bean>
	<bean id="car2" class="com.itzheng.spring.demo4.Car2">
		<property name="name" value="#{carInfo.name}"></property>
		<property name="price" value="#{carInfo.calculatorPrice()}"></property>
	</bean>
	<bean id="employee" class="com.itzheng.spring.demo4.Employee">
		<property name="name" value="#{'武九' }"></property>
		<property name="car2" value="#{car2}"></property>
	</bean>
	<import resource="applicationContext2.xml"/>
</beans>

applicationContext2.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p"
	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">
	<!-- Spring数组属性的注入 -->
	<!-- 注入数组类型 -->
	<bean id="collectionBean" class="com.itzheng.spring.demo5.CollectionBean">
		<!-- 注入数组类型 -->
		<property name="arrs">
			<list>
				<value>王西</value>
				<value>赵娜</value>
				<value>李武</value>
			</list>
		</property>
		<!-- 注入list集合 -->
		<property name="list">
			<list>
				<value>张三</value>
				<value>李四</value>
				<value>登峰</value>
			</list>
		</property>
		<!-- 注入set集合 -->
		<property name="set">
			<set>
				<value>aaa</value>
				<value>bbb</value>
				<value>ccc</value>
				<value>ddd</value>
			</set>
		</property>
		<property name="map">
			<map>
				<entry key="111" value="iiii"></entry>
				<entry key="222" value="zzz"></entry>
				<entry key="333" value="bbb"></entry>
				<entry key="444" value="ddd"></entry>
			</map>
		</property>
	</bean>
</beans>
package com.itzheng.spring.demo5;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/*
 * 复杂类型的属性注入
 */
public class SpringDemo5 {
	@Test
	public void demo1() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		CollectionBean collectionBean = (CollectionBean) applicationContext.getBean("collectionBean");
		System.out.println(collectionBean);
	}
}

在这里插入图片描述

你可能感兴趣的:(Spring,spring,xml,java,ioc)