对象依赖注入 Object Dependency Injection

一、对象依赖注入

  • 依赖注入是指运行时将容器内对象利用反射赋给其他对象的操作

二.对象依赖注入的形式

  • 基于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">

    <!-- 两种对象的依赖注入方式-->
    <!-- setter注入对象-->


    <!-- IOC容器自动利用反射机制运行时调用setter方法为属性赋值 -->
    <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>

    <!-- 利用ref属性注入依赖对象 -->
    <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()
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值