Spring学习---依赖注入(DI)

依赖注入(DI)

构造器注入

在之前IOC创建对象部分有提过
详情看这篇大佬讲解的博客

Set方式注入
  • 依赖:bean对象的创建依赖于容器
  • 注入:bean对象中的所以属性,由容器来注入

【环境搭建】

  1. 复杂类型

    public class Address {
        private String address;
    
        public String getAddress() {
            return address;
        }
        public void setAddress(String address) {
            this.address = address;
        }
    }
    
  2. 真实测试对象

    public class Student {
        private String name;
        private Address address;
        private String[] books; //数组
        private List<String> hobbies;
        private Map<String, String> card;
        private Set<String> games;
    
        private Properties info; //配置类
        private String girlfriend;
    
        //get、set方法
        //toString
    }
    

3.配置文件 beans.xml

<?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="student" class="com.orange.pojo.Student">
        <property name="name" value="guolicheng"/>
    </bean>
</beans>
  1. 测试类

    public class Mytest {
        public static void main(String[] args) {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    
            Student student = (Student) context.getBean("student");
            System.out.println(student.getName()); //输出名字
            System.out.println(student.getAddress()); //null  因为没有set注入
        }
    }
    

完善注入:

<?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="address" class="com.orange.pojo.Address">
        <property name="address" value="北京"/>
    </bean>

    <bean id="student" class="com.orange.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="hobbies">
            <list>
                <value>听jay的歌</value>
                <value>跑步</value>
                <value>编程</value>
                <value>打游戏</value>
            </list>
        </property>

        <!-- Map注入-->
        <property name="card">
            <map>
                <entry key="学生证" value="111222333444"/>
                <entry key="身份证" value="222333444555"/>
                <entry key="借阅证" value="333444555666"/>
            </map>
        </property>

        <!-- Set注入-->
        <property name="games">
            <set>
                <value>QQ飞车</value>
                <value>王者荣耀</value>
                <value>刺激战场</value>
            </set>
        </property>

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

        <!-- Properties注入-->
        <property name="info">
            <props>
                <prop key="学号">19406</prop>
                <prop key="专业">人工智能</prop>
                <prop key="性别"></prop>
                <prop key="年龄">20</prop>
            </props>
        </property>


    </bean>
</beans>
拓展方式注入
1.P命名注入

在配置文件顶部导入:

xmlns:p="http://www.springframework.org/schema/p"
<!--  p命名空间注入  -->
    <bean id="user" class="com.orange.pojo.User" p:name="小明" p:age="19"/>
2.C命名注入

在配置文件顶部导入:

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

显示灰色代表还未使用,c命名前提是得在实体类里需要编写有参和无参。

public User() {
}

public User(String name, int age) {
    this.name = name;
    this.age = age;
}
<!--  命名空间注入  -->
    <bean id="user2" class="com.orange.pojo.User" c:name="小华" c:age="18"/>

测试:

@Test
public void test2(){
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
    User user = (User) context.getBean("user");

    System.out.println(user);
}

注意点:p命名和c命名空间不能直接使用,需要导入xml约束

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

果力成°

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

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

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

打赏作者

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

抵扣说明:

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

余额充值