注入方式
set方法注入
构造函数注入
p名称空间注入
spel注入(#表示理解为引用)
练习代码:
<!-- 第二天 set注入 index:索引 ref:引用对象 type:参数类型--><bean name="person" class="com.spring.bean.Person"><constructor-arg name="name" value="12" index="0" type="java.lang.Integer"></constructor-arg><constructor-arg name="car" ref="car" index="1" ></constructor-arg></bean><bean name="car" class="com.spring.bean.Car"><property name="name" value="兰博基尼"></property><property name="color" value="绿色"></property></bean><!-- p命名空间 --><bean name="person1" class="com.spring.bean.Person" p:name="Alan2" p:age="18" p:car-ref="car"></bean><!-- spel springEL表达式注入 --><bean name="person2" class="com.spring.bean.Person"><property name="name" value="#{person1.name}"></property><property name="age" value="#{person1.age}"></property><property name="car" ref="car"></property></bean>
复杂类型注入
数组
List
Map
Properties
练习代码:
applicationContext.xml
<!-- 复杂类型注入 --><bean name="colleactionsBean" class="com.spring.bean.ColleactionsBean"><!-- 集合中只有一个值 --><!-- <property name="arraylist" value="hello"></property> --><!-- 集合中有多个值 --><property name="array"><array><value>1</value><value>2</value><value>3</value></array></property><property name="list"><list><value>alanList1</value><value>alanList2</value><value>alanList3</value></list></property><property name="map"><map><entry key="alanMap" value="12"></entry><entry key="alan1Map" value="13"></entry><entry key="alan2Map" value="14"></entry></map></property><property name="properties"><props><prop key="prop1">p1</prop><prop key="prop2">p2</prop><prop key="prop3">p3</prop></props></property></bean>
ColleactionsBean.java
package com.spring.bean;import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;public class ColleactionsBean {private int[] array;private List list;private Map map;private Properties properties;public int[] getArray() {return array;}public void setArray(int[] array) {this.array = array;}public List getList() {return list;}public void setList(List list) {this.list = list;}public Map getMap() {return map;}@Overridepublic String toString() {return "ColleactionsBean [array=" + Arrays.toString(array) + ", list=" + list + ", map=" + map + ", properties="+ properties + "]";}public void setMap(Map map) {this.map = map;}public Properties getProperties() {return properties;}public void setProperties(Properties properties) {this.properties = properties;}}
TestDemo.java
package com.test;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.spring.bean.User;public class TestDemo {private ClassPathXmlApplicationContext context;@Testpublic void testDemo() throws InterruptedException{context = new ClassPathXmlApplicationContext("applicationContext.xml");/*scope=prototype的javaBean在创建容器时候不会被创建*/User bean = (User)context.getBean("user");//User bean1 = (User) context.getBean("user");//System.out.println(bean==bean1);bean.setName("DreamZuora");bean.setAge(18);System.out.println(bean);/*单例对象可以销毁,多例对象不会被销毁*/context.close();}}