spring 的依赖注入简单和复杂类型

一、XML配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://www.springframework.org/schema/beans" 
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">

			<!-- set方式注入: -->
		<bean name="user" class="com.lzp.bean.User">
			<!-- 值类型注入:为User对象中名为name的属性注入jack作为值 -->
			<property name="name" value="jack"></property>
			<property name="age" value="21"></property>
			<!-- 引用类型注入:为maserati属性注入下方配置的maseratis对象 -->
			<property name="maserati" ref="maseratis"></property>
		</bean>
		
		<!-- 将Maserati对象配置容器中 -->
		<bean name="maseratis" class="com.lzp.bean.Maserati">
			<property name="name" value="宝马"></property>
			<property name="color" value="钻石"></property>
		</bean>
		
		<!-- 构造函数注入 -->
		<!-- 当有多个构造函数时可以用index或type属性指定用那个构造函
				index="0":当前表示User对象age属性在构造函数中在第一个位置
				index="1":当前表示User对象name属性在构造函数中在第二个位置
				type:通过参数类型来确定走的那个构造-->
		<bean name="user2" class="com.lzp.bean.User">
			<constructor-arg name="name" value="007" index="1" type="java.lang.Integer"></constructor-arg>
			<constructor-arg name="age" value="17" index="0"></constructor-arg>
			<constructor-arg name="maserati" ref="maseratis" index="2"></constructor-arg>
		</bean>
		
		<!-- p名称空间注入,其实还是走的set方法,spring新开发出来的注入方式
			前提条件:导入p名称空间的引入地址xmlns:p="http://www.springframework.org/schema/p"
					  直接加入到beans根节点中去就OK 
			注入方法:值属性方式:p:属性值="值"
					  对象属性方式:p:属性名-ref="bean名称"-->
		<bean name="user3" class="com.lzp.bean.User" p:name="tom" p:age="177" p:maserati-ref="maseratis">
		
		</bean>
		<!-- spel注入:spring Expression Language spinge表达语言
			使用方法:value值可以获得其它对象的值来给当前对象属性赋值 -->
		<bean name="user4" class="com.lzp.bean.User">
										<!--获得user对象name的值  -->
			<property name="name" value="#{user.name}"></property>
										<!--获得user2对象age的值  -->
			<property name="age" value="#{user2.age}"></property>
			<property name="maserati"  ref="maseratis"></property>
		</bean>
		
		
		<!-- 复杂类型的注入 -->
		<bean name="coll" class="com.lzp.bean.CollectionBean">
			<!-- 当数组中只准备注入一个值或对象时,可以直接使用value|ref -->
			<!-- <property name="arr" value="lee"></property> 一个值情况-->
			<property name="arr">
				<array>
					<value>brucelee</value>
					<value>32</value>
					<ref bean="user"/>
				</array>
			</property>
			
			<!-- 当list注入一个值或对象时,可以直接使用value|ref -->
			<!-- <property name="list" value="marry"></property> 一个值情况-->
				<property name="list">
					<list>
						<value>mark</value>
						<value>11</value>
						<ref bean="user2"/>
					</list>
				</property>
				
			<!-- 当Map注入值或对象时,可以直接使用value|ref -->	
				<property name="map">
					<map>
						<!-- key:字符串     value:字符串 -->
						<entry key="name" value="tom"></entry>
						<!-- key:字符串     value:对象 -->
						<entry key="user" value-ref="user2"></entry>
						<!-- key:对象     value:对象 -->
						<entry key-ref="user" value-ref="user3"></entry>
					</map>
				</property>
				
			<!--  properties类型注入-->
			<property name="pro">
				<props>
					<prop key="lzp">lee</prop>
					<prop key="tom">mark</prop>
					<prop key="lee">brucelee</prop>
				</props>
			</property>
		</bean>
</beans>

二、测试类

package com.lzp.injection;

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

import com.lzp.bean.CollectionBean;
import com.lzp.bean.User;

public class Demo {
		//创建方式一,空参构造创建
		@Test
		public void test() {
		//1、创建容器对象
		ApplicationContext app = new ClassPathXmlApplicationContext("com/lzp/injection/applicationContext.xml");
		//2、向容器“要”user对象
		CollectionBean bean = (CollectionBean) app.getBean("coll"); 
		//打印user对象
		System.out.println(bean);
	}

}

猜你喜欢

转载自blog.csdn.net/lzpzwy/article/details/80706797