springDI依赖注入

在这里插入图片描述
1、构造器注入
请参考前面一篇博客IOC创建对象的方式及spring配置

2、set方式注入(重点)
依赖注入:Set注入

依赖:bean对象的创建依赖于容器!
注入:bean对象中的所有属性,由容器来注入!

环境搭建
1、复杂类型:

public class Address {
    //引用类型
    private String address;

    public String getAddress() {
        return address;
    }

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

    @Override
    public String toString() {
        return "Address{" +
                "address='" + address + '\'' +
                '}';
    }
}

2、测试对象:

public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties info;
}

3、beans.xml(重点)

<bean id="address" class="com.like.pojo.Address">
    <property name="address" value="重庆"/>
</bean>
<bean id="student" class="com.like.pojo.Student">
    <!--普通值注入,value-->
    <property name="name" value="玛卡巴卡"/>

    <!--bean注入、ref-->
    <property name="address" ref="address"/>

    <!--数组-->
    <property name="books">
        <array>
            <value>西游记</value>
            <value>三国演义</value>
            <value>水浒传</value>
            <value>红楼梦</value>
        </array>
    </property>

    <!--List-->
    <property name="hobbys">
        <list>
            <value>唱歌</value>
            <value>跳舞</value>
            <value>rap</value>
        </list>
    </property>

    <!--Map-->
    <property name="card">
        <map>
            <entry key="身份证" value="200001010001"/>
            <entry key="银行卡" value="230274747023"/>
        </map>
    </property>

    <!--Set-->
    <property name="games">
        <set>
            <value>打篮球</value>
            <value>LOL</value>
            <value>NBA</value>
        </set>
    </property>

    <!--Null-->
    <property name="wife">
        <null/>
    </property>

    <!--Properties-->
    <property name="info">
        <props>
            <prop key="driver">696969669</prop>
            <prop key="url">232.3543.2343</prop>
            <prop key="user">root</prop>
            <prop key="password">991123</prop>
        </props>
    </property>
</bean>

Result:

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());

        /*
        Student{name='玛卡巴卡',
        address=Address{address='重庆'},
        books=[西游记, 三国演义, 水浒传, 红楼梦],
        hobbys=[唱歌, 跳舞, rap], card={身份证=200001010001, 银行卡=230274747023},
        games=[打篮球, LOL, NBA],
        wife='null',
        info={user=root, password=991123, url=232.3543.2343, driver=696969669}}
        */
    }
}

3、拓展方式注入
可以使用p命名空间和c命名空间进行注入
在这里插入图片描述
使用,去官网导入xml约束!

xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"

报红的话可以这样解决:
打开File------->Settings------>Languages & Frameworks
在这里插入图片描述
使用:

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

    <!--p命名空间注入,可以直接注入属性的值-->
    <bean id="user" class="com.like.pojo.User" p:name="武松" p:age="3"/>
    <!--c命名空间注入,通过构造器注入:constructor-args-->
    <bean id="user2" class="com.like.pojo.User" c:age="5" c:name="李逵"/>
</beans>

测试:

@Test
public void test(){
    ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
    User user = context.getBean("user2", User.class);
    System.out.println(user);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Marlboro~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值