Spring学习笔记-6-依赖注入


依赖注入.

目录结构

1、构造器注入.

直接通过参数名(推荐) 还有两种(索引,和类型).

<!--按名字 beans.xml-->
<bean id="user" class="pojo.User">
    <constructor-arg name="id" value="3"/>
    <constructor-arg name="name" value="liuyou"/>
    <constructor-arg name="pwd" value="password"/>
</bean>

2、setter注入(重点).

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

环境搭建.

1.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">

</beans>
2.实体类.
@Data
public class Student {

    private String name;//value
    private Address address;//ref 复杂类型
    private String[] books;//array
    private List<String> hobbies;//list
    private Map<String, String> card;//map
    private Set<String> games;//set
    private Properties info;//properties
    private String wife; // null
}
@Data
class Address{
    private String address;
}
3.setter注入方式(beans.xml中beans标签下).
<!-- setter注入方式 -->
<bean id="student" class="pojo.Student">
    <!-- 1.普通值value -->
    <property name="name" value="刘民锴"/>
    <!-- 2.bean注入 ref -->
    <property name="address" ref="address"/>
    <!-- 3.array注入 -->
    <property name="books">
        <array>
            <value>计算机网络</value>
            <value>计算机操作系统</value>
            <value>数据结构与算法</value>
        </array>
    </property>
    <!-- 4.list注入 -->
    <property name="hobbies">
        <list>
            <value>爱编程</value>
            <value>爱漫画</value>
        </list>
    </property>
    <!-- 5.map注入 -->
    <property name="card">
        <map>
            <entry key="身份证" value="1919199191919"/>
            <entry key="银行卡" value="1919199191919"/>
        </map>
    </property>
    <!-- 6.Set注入 -->
    <property name="games">
        <set>
            <value>欢乐斗地主</value>
            <value>王者荣耀</value>
            <value>中国象棋</value>
        </set>
    </property>
    <!-- 7.prop注入 -->
    <property name="info">
        <props>
            <prop key="性别"></prop>
        </props>
    </property>
    <!-- 8.null注入 -->
    <property name="wife">
        <null/>
    </property>
</bean>

<bean id="address" class="pojo.Address">
    <property name="address" value="武汉文华学院"/>
</bean>
4.测试类.
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = context.getBean("student", Student.class);

        System.out.println(student);

    }
}
5.测试结果.

3、其他方式注入.

  • p-namespace(p命名空间注入)

    • p(property,可直接注入属性的值)

    • xmlns:p="http://www.springframework.org/schema/p"
      <bean id="student" class="pojo.Student" p:name="刘民锴"/>
  • c-namespace(c命名空间注入)

    • c(constructor,可直接对有参构造器进行注入)

    • xmlns:c="http://www.springframework.org/schema/c"
      <bean id="student" class="pojo.Student" c:constructorParam="value"/>

文章作者: liuminkai
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 liuminkai !
评论
  目录