当前位置:   article > 正文

IOC创建对象的几种方式_ioc如何创建对象

ioc如何创建对象


依赖注入:
依赖 : 指Bean对象的创建依赖于容器 。 Bean对象的依赖资源。
注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配 。

一:构造器注入

1:通过无参构造方法来创建( 实质通过set注入创建 )

实体类:

public class User {
    private String name;

    public User() {
        System.out.println("无参构造方法");
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

bean配置文件:

<?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="user" class="com.lmy.pojo.User">
       <property name="name" value="zhangSan"/>
    </bean>
</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

测试:

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        User user = (User) context.getBean("user");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

输出:

无参构造方法

Process finished with exit code 0
  • 1
  • 2
  • 3

当没有无参构造方法时:

public class User2 {
    private String name;

    public User2(String name) {
        this.name = name;
        System.out.println("有参构造方法");
    }

    public String getName() {
        return name;
    }

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

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

配置报错:
在这里插入图片描述

总结: 默认的创建方式即通过无参构造方法创建,在getBean获取容器对象时对象已经创建了

2:通过有参构造方法来创建

实体类:

public class User2 {
    private String name;

    public User2(String name) {
        this.name = name;
        System.out.println("有参构造方法");
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

bean配置文件:

<?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">

    <!-- 第一种根据index参数下标设置 -->
    <bean id="user2" class="com.lmy.pojo.User2">
        <!-- index指构造方法 , 下标从0开始 -->
       <constructor-arg index="0" value="zhangSan2"/>
    </bean>

    <!-- 第二种根据参数名字设置 -->
    <bean id="user3" class="com.lmy.pojo.User2">
        <!-- name指参数名 -->
        <constructor-arg name="name" value="zhangSan3"/>
    </bean>

    <!-- 第三种根据参数类型设置 -->
    <bean id="user4" class="com.lmy.pojo.User2">
        <constructor-arg type="java.lang.String" value="zhangSan4"/>
    </bean>
</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

测试:

public class MyTest2 {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans2.xml");
        User2 user2 = (User2) context.getBean("user2");
        User2 user3 = (User2) context.getBean("user3");
        User2 user4 = (User2) context.getBean("user4");
        System.out.println(user2);
        System.out.println(user3);
        System.out.println(user4);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

结果:

有参构造方法
有参构造方法
有参构造方法
User{name='zhangSan2'}
User{name='zhangSan3'}
User{name='zhangSan4'}

Process finished with exit code 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

结论:
在配置文件加载的时候。其中管理的对象都已经初始化了!
注意:

  1. 根据参数类型来设置参数时,基本类型可直接用,但引用类型需要将类型写成全限定类名,否则报错
  2. 不建议使用根据参数类型来设置参数这种方式,因为当实体中有多个相同类型的参数时这种方式将不可用

二:set注入 (重点)

注意:要求被注入的属性 , 必须有set方法, set方法的方法名由set + 属性首字母大写 , 如果属性是boolean类型 , 没有set方法 , 是 is 。
实体类:

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 + '\'' +
                '}';
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
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 String wife;
    private Properties info;

    public String getName() {
        return name;
    }

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

    public Address getAddress() {
        return address;
    }

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

    public String[] getBooks() {
        return books;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }

    public List<String> getHobbies() {
        return hobbies;
    }

    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }

    public Map<String, String> getCard() {
        return card;
    }

    public void setCard(Map<String, String> card) {
        this.card = card;
    }

    public Set<String> getGames() {
        return games;
    }

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

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    public Properties getInfo() {
        return info;
    }

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

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address +
                ", books=" + Arrays.toString(books) +
                ", hobbies=" + hobbies +
                ", card=" + card +
                ", games=" + games +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89

配置类:

<?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.lmy.pojo.Address">
        <property name="address" value="重庆"/>
    </bean>

    <bean id="student" class="com.lmy.pojo.Student">
        <!--常量注入-->
        <property name="name" value="zhangSan"/>
        <!--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>唱歌</value>
                <value>跳舞</value>
            </list>
        </property>
        <!--Map注入-->
        <property name="card">
            <map>
                <entry key="手机卡" value="12345678912"/>
                <entry key="身份证" value="123456789123456789"/>
            </map>
        </property>
        <!--set注入-->
        <property name="games">
            <set>
                <value>GTA5</value>
                <value>王者荣耀</value>
            </set>
        </property>
        <!--Null注入-->
        <property name="wife">
            <null/>
        </property>
        <!--Properties注入-->
        <property name="info">
            <props>
                <prop key="学号">123</prop>
                <prop key="性别"></prop>
            </props>
        </property>
    </bean>
</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

测试:

public class MyTest {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

结果:

Student{name='zhangSan', address=Address{address='重庆'}, books=[红楼梦, 西游记, 水浒传, 三国演义], hobbies=[唱歌, 跳舞], card={手机卡=12345678912, 身份证=123456789123456789}, games=[GTA5, 王者荣耀], wife='null', info={学号=123, 性别=}}

Process finished with exit code 0
  • 1
  • 2
  • 3

三:“c命名空间注入”和“p命名空间注入”

实体类:

public class User {
    private String name;
    private int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

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

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

配置文件:

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

    <!--
       需要导入约束 : xmlns:p="http://www.springframework.org/schema/p"
       P(属性: properties)命名空间 , 属性依然要设置set方法
    -->
    <bean id="user" class="com.lmy.pojo.User" p:name="zhangShan" p:age="21"/>

    <!--
      导入约束 : xmlns:c="http://www.springframework.org/schema/c"
      C(构造: Constructor)命名空间 , 属性依然要设置有参构造方法
    -->
    <bean id="user1" class="com.lmy.pojo.User" c:name="lisi" c:age="21"/>
</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

测试:

public class MyUserTest {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        User user = (User) context.getBean("user1");
        System.out.println(user.toString());
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

输出:

User{name='lisi', age=21}

Process finished with exit code 0
  • 1
  • 2
  • 3

结论:
P(属性: properties)命名空间注入即类似于set注入
C(构造: Constructor)命名空间注入即类似于有参构造器注入
注意:
有参构造器注入set注入不能同时用于同一个实体类。因为实体类中有有参构造器后只能使用有参构造器注入,不能使用set注入

四:注解

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读