0
点赞
收藏
分享

微信扫一扫

Spring 的依赖注入

cwq聖泉寒江2020 2022-01-31 阅读 109

Spring 的依赖注入

通过set 方法进行依赖的注入

当给属性赋值的时候,必须提供该属性的set 方法。
Address.java

package com.momo.entity;

public class Address {
    private String address;

    public Address() {
        
    }

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

    public String getAddress() {
        return address;
    }

   

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

Students.java

package com.momo.entity;

import java.util.*;

public class Student {
    private String name;
    private Address outBean; // 外部bean
    private Address innerBean;// 内部bean
    private String[] array;
    private List<String> list;
    private Map<String,String> map;
    private Set<String> set;
    private String emptyValue; // 注入一个 空字符串
    private String nullValue; // 注入一个null 值
    private Properties props;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", outBean=" + outBean +
                ", innerBean=" + innerBean +
                ", array=" + Arrays.toString(array) +
                ", list=" + list +
                ", map=" + map +
                ", set=" + set +
                ", emptyValue='" + emptyValue + '\'' +
                ", nullValue='" + nullValue + '\'' +
                ", props=" + props +
                '}';
    }
}

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
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="address" class="com.momo.entity.Address">
        <property name="address" value="北京市"/>
    </bean>

    <bean id="student" name="s" class="com.momo.entity.Student">
<!--       1.常量的注入-->
        <property name="name" value="Mo"/>
<!--        2.外部bean 的方式来注入-->
        <property name="outBean" ref="address"/>
<!--        3.内部bean 的注入-->
        <property name="innerBean">
            <bean class="com.momo.entity.Address">
                <property name="address" value="北京市"/>
            </bean>
        </property>
<!--        4.数组-->
        <property name="array">
            <array>
                <value>Mybatis</value>
                <value>Spring</value>
                <value>SpringMVC</value>
            </array>
        </property>

<!--        5.list-->
        <property name="list">
            <list>
                <value>Mybatis</value>
                <value>Spring</value>
                <value>SpringMVC</value>
            </list>
        </property>
<!--        6.map-->
        <property name="map">
            <map>
                <entry key="1001" value="张三"/>
                <entry key="1002" value="李四"/>
            </map>
        </property>

<!--        7.set-->
        <property name="set">
            <set>
                <value>Mybatis</value>
                <value>Spring</value>
                <value>SpringMVC</value>
            </set>
        </property>
<!--        8.空字符串-->
        <property name="emptyValue">
            <value/>
        </property>

<!--        9.null-->
        <property name="nullValue">
            <null/>
        </property>
<!--        10.properties-->
        <property name="props">
            <props>
                <prop key="学号">1001</prop>
                <prop key="性别">男</prop>
                <prop key="姓名">张三</prop>
            </props>
        </property>
    </bean>
</beans>

测试

package com.momo.test;

import com.momo.entity.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Mytest {

    @Test
    public void test(){
        ApplicationContext context =
                new ClassPathXmlApplicationContext("beans.xml");
        Student s = (Student) context.getBean("s");
        System.out.println(s);

    }
}

通过构造方法进行注入

传统的构造方法可以给属性赋值。
this.xxx = xxx; // 手动赋值。需要手动传参。
实体类:

package com.momo.entity;

import java.util.Date;

public class User {
    private String name;
    private Integer age;
    private Date birthday;
    
    public User(){
        System.out.println("无参数的构造方法");
    }
    
    public User(Integer age){
        System.out.println("有一个Integer类型的参数的构造");
        this.age = age;
    }
    
    public User(String name){
        System.out.println("有一个String类型的参数的构造");
        this.name = name;
    }
    
    public User(String name,Integer age){
        System.out.println("有2个String类型和Integer类型的参数的构造");
        this.age = age;
        this.name = name;
    }

    public User(String name, Integer age, Date birthday) {
        System.out.println("3个参数参数的构造方法");
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                '}';
    }
}

1.通过下标

<bean id="user"  class="com.momo.entity.User">
        <constructor-arg index="0" value="张三"/>
        <constructor-arg index="1" value="19"/>
    </bean>

index 属性 : 就是第几个属性。  给对应的属性赋值的时候,会默认的找到适合当前赋值的构造。

2.通过参数类型

 <bean id="user"  class="com.momo.entity.User">
        <constructor-arg type="java.lang.Integer" value="19"/>
        <constructor-arg type="java.lang.String" value="张三"/>
</bean>

可以用,但是不推荐使用。

3.通过参数的名称

<bean id="user"  class="com.momo.entity.User">
        <constructor-arg name="age"  value="19"/>
        <constructor-arg name="name"  value="张三"/>
        <constructor-arg name="birthday" ref="now"/>
</bean>

<bean id="now" class="java.util.Date"/>

通过p命名空间来注入

本质是 通过调用 set 方法和构造方法来进行注入

1.通过p命名空间来注入,需要在beans.xml 中导入 p 命名空间。

在文件的开头加入约束:
  xmlns:p="http://www.springframework.org/schema/p"
 <bean id="address" class="com.momo.entity.Address"

    p:address="北京市"/>


<bean id="student" name="s" class="com.momo.entity.Student"
    p:outBean-ref="address">
  
  
总结:
  1.p命名空间注入的时候,利用类的set方法
  2.注入的时候分2种情况
  		1).基本类型和String--->在bean的标签上加上属性 p:需要注入的属性的属性名="值"
  		2).注入外部的对象---->在bean的标签上加上属性 p:需要注入的属性的属性名-ref = "外部bean的id"

通过c命名空间来注入

本质上还是使用构造方法来注入。

		导入约束
       xmlns:c="http://www.springframework.org/schema/c"
<bean id="user"  class="com.momo.entity.User" c:name="张三"/>

注意: 需要提供 name的构造

 public User(String name){
        System.out.println("有一个String类型的参数的构造");
        this.name = name;
    }
举报

相关推荐

0 条评论