Spring——依赖注入

依赖注入

内容:依赖注入(DI)是一个过程,通过该过程,对象只能通过构造函数参数,或创建对象实例后在对象实例上设置的属性来定义其依赖关系。

实例:当Class A中用到了 Class B,这时需要在A中new 一个B的对象b。当使用依赖注入时,只需要在A中定义一个私有的B的对象,然后通过IOC容器new 一个B的对象b,再把b注入到A中来使用。

好处:

  1. 降低代码的耦合性
  2. 增强了模块的重用性以及灵活性
1. 基于构造器的依赖注入

实体类

public class User {
    private String name;

    public User() {
        System.out.println("执行了User类的无参构造方法~");
    }

    public User(String name){
        this.name = name;
        System.out.println("执行了User类的有参构造方法");
    }

    //必须要设置set方法,因为注入时 需要通过set方法注入
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }
}
  1. 通过下标方式注入(通过index来选择,给有参构造的第几个参数注入
	<bean id="user" class="com.test.pojo.User">
        <constructor-arg index="0" value="gyp"/>
    </bean>
  1. 通过名字注入
    <bean id="user" class="com.test.pojo.User">
        <constructor-arg name="name" value="gyp"/>
    </bean>
  1. 通过类型注入(不建议使用!因为当类里面有两个相同类型的属性时,无法给属性注入
	<bean id="user" class="com.test.pojo.User">
        <constructor-arg type="java.lang.String" value="gyp"/>
    </bean>
2. 基于setter的依赖注入

实体类

public class Student {
    private String name;
    private Address address;
    private String[] courses;
    private List<String> hobbys;
    private Map<String,String> games;
    private Set<String> associations;
    private Properties info;

    public void setInfo(Properties info) {
        this.info = info;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public void setCourses(String[] courses) {
        this.courses = courses;
    }

    public void setHobbys(List<String> hobbys) {
        this.hobbys = hobbys;
    }

    public void setGames(Map<String, String> games) {
        this.games = games;
    }

    public void setAssociations(Set<String> associations) {
        this.associations = associations;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                "\n address=" + address +
                "\n courses=" + Arrays.toString(courses) +
                "\n hobbys=" + hobbys +
                "\n games=" + games +
                "\n associations=" + associations +
                "\n info=" + info +
                '}';
    }
}

Address类 bean注入测试时,使用的bean

public class Address {
    private Address address;

    public void setAddress(Address address) {
        this.address = address;
    }
}

}

  1. 普通值
<property name="name" value="gyp"/>
  1. Bean
<property name="address" ref="address"/>
  1. 数组
<property name="courses">
    <array>
        <value>C语言</value>
        <value>C++</value>
        <value>Java</value>
    </array>
</property>
  1. list
<property name="hobbys">
    <list>
        <value>打游戏</value>
        <value>打篮球</value>
        <value>听歌</value>
    </list>
</property>
  1. map
<property name="games">
    <map>
        <entry key="LOL" value="英雄联盟"/>
        <entry key="CS" value="反恐精英"/>
        <entry key="CF" value="穿越火线"/>
    </map>
</property>
  1. set
<property name="associations">
    <set>
        <value>计算机协会</value>
        <value>魔术协会</value>
        <value>英语协会</value>
    </set>
</property>
  1. properties
<property name="info">
    <props>
        <prop key="身份证">152323</prop>
        <prop key="银行卡">232323</prop>
        <prop key="手机号">156563</prop>
    </props>
</property>
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值