一、对象依赖注入
- 依赖注入是指运行时将容器内对象利用反射赋给其他对象的操作
二.对象依赖注入的形式
- 基于setter方法注入对象
- 基于构造方法的注入对象
1.基于setter方法注入(静态数据)对象
package com.learn.spring.ioc.entity;
public class Apple {
private String title;
private String color;
private String origin;
private Float price;
public Apple() {
System.out.println("正在创建对象:" + this);
}
public Apple(String title, String color, String origin, Float price) {
this.title = title;
this.color = color;
this.origin = origin;
this.price = price;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
System.out.println("title属性设置为: " + title);
this.title = title;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getOrigin() {
return origin;
}
public void setOrigin(String origin) {
this.origin = origin;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
}
package com.learn.spring.ioc.entity;
public class Child {
private String name;
private Apple apple;
public Child() {
}
public Child(String name, Apple apple) {
this.name = name;
this.apple = apple;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Apple getApple() {
return apple;
}
public void setApple(Apple apple) {
System.out.println("注入的Apple对象:" + apple);
this.apple = apple;
}
public void eat(){
System.out.println(name + "吃到了" + apple.getOrigin() + "种植的" + apple.getTitle());
}
}
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="sweetApple" class="com.learn.spring.ioc.entity.Apple">
<property name="title" value="红富士"></property>
<property name="origin" value="欧洲"></property>
<property name="color" value="红色"></property>
<property name="price" value="19.8"></property>
</bean>
<bean id="lily" class="com.learn.spring.ioc.entity.Child">
<property name="name" value="莉莉"></property>
<property name="apple" ref="sweetApple"></property>
</bean>
</beans>
package com.learn.spring.ioc;
import com.learn.spring.ioc.entity.Apple;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringApplicationDI {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext-DI.xml");
Apple sweetApple = context.getBean("sweetApple", Apple.class);
System.out.println(sweetApple.getTitle());
}
}
2.基于构造方法注入(动态)对象
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="sourApple" class="com.learn.spring.ioc.entity.Apple">
<property name="title" value="青苹果"></property>
<property name="origin" value="中亚"></property>
<property name="color" value="绿色"></property>
<property name="price" value="9.8"></property>
</bean>
<bean id="andy" class="com.learn.spring.ioc.entity.Child">
<constructor-arg name="name" value="安迪"/>
<constructor-arg name="apple" ref="sourApple"/>
</bean>
</beans>
package com.learn.spring.ioc;
import com.learn.spring.ioc.entity.Apple;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringApplicationDI {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext-DI.xml");
Apple sourApple = context.getBean("sourApple", Apple.class);
System.out.println(sourApple.getTitle());
}
}
二、注入集合对象
1.注入List (允许数据出现重复)
<bean id="" class="">
<property name="someList">
<list>
<value>具体值</value>
<ref bean="beanId"></ref>
</list>
</property>
</bean>
2.注入Set (不允许数据出现重复,如果出现重复数据,会自动去重)
<bean id="" class="">
<property name="someSet">
<set>
<value>具体值</value>
<ref bean="beanId"></ref>
</set>
</property>
</bean>
3.注入Map (map中的key 必须是字符串,value可以式数字或字符串,也可以是其它对象的引用)
<bean id="" class="">
<property>
<map>
<entry key="k1" value="v1"></entry>
<entry key="k2" value="beanId"></entry>
</map>
</property>
</bean>
4.注入Properties(key与value必须是字符串)
<bean>
<property name="someProperties">
<props>
<prop key="k1">v1</prop>
<prop key="k2">v2</prop>
</props>
</property>
</bean>
三、查看容器对象
获取容器内所有的beanId数组 ApplicationContextObject.getBeanDefinitionNames()
查看Bean类型 ApplicationContextObject.getBean(beanName).getClass().getName()
查看Bean内容 ApplicationContextObject.getBean(beanName).toString()