Spring创建对象的方式--03

通过构造函数创建对象。 最基本的对象创建方式,只需要有一个无参构造函数(类中一般来说会有默认的构造函数,如果写了任何一个构造函数,默认的无参构造函数就不会自动创!!!)和字段的setter方法。 SpringContext利用无参的构造函数创建一个对象,然后利用setter方法赋值。

上述文字太过专业化,理解起来有一些费劲,那么跟着我一起来拆解他:

一 理解无参构造

首先我们创建一个pojo类,具体如下:

package com.j.pojo;

/**
 * Created by jdx on 2022/1/4 下午9:31
 */
public class Students {
    //定义一个字符变量
    private String name;

    //无参构造
    public Students() {
        System.out.println("student的无参构造函数!!!");
    }

    //get set方法

    public String getName() {
        return name;
    }

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

    //定义一个方法
    public void examination() {
        System.out.println("StudentsName="+name);
    }
}

接下来我们编写一个测试类:

import com.j.pojo.Students;

/**
 * Created by jdx on 2022/1/4 下午9:37
 */
public class IOCText {
    public static void main(String[] args) {
        Students students = new Students();
    }
}

运行结果为:

student的无参构造函数!!!

其中不难看出,无参构造其实就是java默认实例化的一个方法。那么到这里,我想各位对开头引用的无参构造大致有了一个了解.

二 IOC创建对象之无参构造

我们在第一步已然创建了实例化的对象,如果说springContext来创建一个实例化对象在通过setter方法将值赋,那么这个过程就是IOC创建对象。

IOC无参构造实例

为了减少篇幅,直接使用Students这个pojo类,
接下来创建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">

    <!--使用Spring来创建对象,在Spring中这些都称之为Bean-->

    <!--id可以理解为区别其他bean的标识,class指的是对象路径-->
<!--    <bean id="helloSpring" class="com.j.pojo.HelloSpring">-->
<!--        &lt;!&ndash;property:属性   name:属性名   value:属性值&ndash;&gt;-->
<!--        <property name="string" value="Spring"/>-->
<!--    </bean>-->

    <bean id="studentName" class="com.j.pojo.Students">
        <property name="name" value="ruck"/>
    </bean>
</beans>

然后,编写测试类:

import com.j.pojo.Students;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by jdx on 2022/1/4 下午9:37
 */
public class IOCText {
    public static void main(String[] args) {
        //获取ioc容器
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //从容器中取出所需对象
        Students studentName = (Students) context.getBean("studentName");
        //调用对象方法
        studentName.examination();
    }
}

输出结果为:

student的无参构造函数!!!
StudentsName=ruck

三 IOC创建对象之有参构造

在正常的开发中,一个对象会有无参构造,但是相应的项目中的构造函数不可能都是无参构造,那么我们试着将无参构造在上述例子中干掉👿,然后尝试着会发生什么。

找到pojo类Students,然后将无参构造修改为有参构造,代码如下:

package com.j.pojo;

/**
 * Created by jdx on 2022/1/4 下午9:31
 */
public class Students {
    //定义一个字符变量
    private String name;

    //有参构造
    public Students(String name) {
        this.name = name;
    }

    //get set方法

    public String getName() {
        return name;
    }

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

    //定义一个方法
    public void examination() {
        System.out.println("StudentsName=" + name);
    }
}

我们在此运行测试类,结果如下:

Caused by: java.lang.NoSuchMethodException: com.j.pojo.Students.<init>()
	at java.base/java.lang.Class.getConstructor0(Class.java:3349)
	at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2553)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78)
	... 14 more

出现这个结果是因为,没有无参构造方法,导致对象无法进行实例化。那么我们应该怎么解决这个问题?怀着这个问题,我们来探究一下有参构造是如何构造对象的。

首先我们可以在官网上面去查看有哪些方法去解决,当然在这里我也就不墨迹直接上代码;

根据下标赋值

沿用上述实例
修改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">

    <!--使用Spring来创建对象,在Spring中这些都称之为Bean-->

    <!--id可以理解为区别其他bean的标识,class指的是对象路径-->
<!--    <bean id="helloSpring" class="com.j.pojo.HelloSpring">-->
<!--        &lt;!&ndash;property:属性   name:属性名   value:属性值&ndash;&gt;-->
<!--        <property name="string" value="Spring"/>-->
<!--    </bean>-->

<!--    <bean id="studentName" class="com.j.pojo.Students">-->
<!--        <property name="name" value="ruck"/>-->
<!--    </bean>-->
    <!--有参构造-->

    <!--第一种方式下标赋值-->
    <bean id="studentName" class="com.j.pojo.Students">
        <!--下标法:根据下标,去给对象属性赋值-->
        <constructor-arg index="0" value="Modi"/>
    </bean>
</beans>

再次运行测试类:

import com.j.pojo.Students;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by jdx on 2022/1/4 下午9:37
 */
public class IOCText {
    public static void main(String[] args) {
        //获取ioc容器
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //从容器中取出所需对象
        Students studentName = (Students) context.getBean("studentName");
        //调用对象方法
        studentName.examination();
    }
}

运行结果为:

StudentsName=Modi

根据类型赋值

沿用上述实例
修改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">

    <!--使用Spring来创建对象,在Spring中这些都称之为Bean-->

    <!--id可以理解为区别其他bean的标识,class指的是对象路径-->
<!--    <bean id="helloSpring" class="com.j.pojo.HelloSpring">-->
<!--        &lt;!&ndash;property:属性   name:属性名   value:属性值&ndash;&gt;-->
<!--        <property name="string" value="Spring"/>-->
<!--    </bean>-->

<!--    <bean id="studentName" class="com.j.pojo.Students">-->
<!--        <property name="name" value="ruck"/>-->
<!--    </bean>-->
    <!--有参构造-->

<!--    &lt;!&ndash;第一种方式下标赋值&ndash;&gt;-->
<!--    <bean id="studentName" class="com.j.pojo.Students">-->
<!--        &lt;!&ndash;下标法:根据下标,去给对象属性赋值&ndash;&gt;-->
<!--        <constructor-arg index="0" value="Modi"/>-->
<!--    </bean>-->

    <!--第二种方式属性赋值-->
    <bean id="studentName" class="com.j.pojo.Students">
        <!--type:属性类型,value:去给对象属性赋值-->
        <constructor-arg type="java.lang.String" value="HuaChenYu"/>
    </bean>
</beans>

再次运行测试类:

import com.j.pojo.Students;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by jdx on 2022/1/4 下午9:37
 */
public class IOCText {
    public static void main(String[] args) {
        //获取ioc容器
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //从容器中取出所需对象
        Students studentName = (Students) context.getBean("studentName");
        //调用对象方法
        studentName.examination();
    }
}

运行结果:

StudentsName=HuaChenYu

不 建 议 使 用 \color{#FF0000}{不建议使用} 使

根据属性名称赋值

沿用上述实例
修改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">

    <!--使用Spring来创建对象,在Spring中这些都称之为Bean-->

    <!--id可以理解为区别其他bean的标识,class指的是对象路径-->
<!--    <bean id="helloSpring" class="com.j.pojo.HelloSpring">-->
<!--        &lt;!&ndash;property:属性   name:属性名   value:属性值&ndash;&gt;-->
<!--        <property name="string" value="Spring"/>-->
<!--    </bean>-->

<!--    <bean id="studentName" class="com.j.pojo.Students">-->
<!--        <property name="name" value="ruck"/>-->
<!--    </bean>-->
    <!--有参构造-->

<!--    &lt;!&ndash;第一种方式下标赋值&ndash;&gt;-->
<!--    <bean id="studentName" class="com.j.pojo.Students">-->
<!--        &lt;!&ndash;下标法:根据下标,去给对象属性赋值&ndash;&gt;-->
<!--        <constructor-arg index="0" value="Modi"/>-->
<!--    </bean>-->

<!--    &lt;!&ndash;第二种方式下标赋值&ndash;&gt;-->
<!--    <bean id="studentName" class="com.j.pojo.Students">-->
<!--        &lt;!&ndash;type:属性类型,value:去给对象属性赋值&ndash;&gt;-->
<!--        <constructor-arg type="java.lang.String" value="HuaChenYu"/>-->
<!--    </bean>-->

    <!--第三种方式:下标赋值-->
    <bean id="studentName" class="com.j.pojo.Students">
        <!--name:属性名称,value:去给对象属性赋值-->
        <constructor-arg name="name" value="DaYou"/>
    </bean>
</beans>

再次运行测试类:

import com.j.pojo.Students;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by jdx on 2022/1/4 下午9:37
 */
public class IOCText {
    public static void main(String[] args) {
        //获取ioc容器
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //从容器中取出所需对象
        Students studentName = (Students) context.getBean("studentName");
        //调用对象方法
        studentName.examination();
    }
}

运行结果为:

StudentsName=DaYou

四 总结

Spring容器说白了其实就是将对象实例化,(在加载beans.xml的时候),用的时候直接拿来使用即可。
而且不管注册多少个beans在同一个beans文件下都是同一个对象。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

敏姐儿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值