Ioc’s method of creating objects is divided into no-parameter construction and no-parameter construction. First, let’s look at no-parameter construction, which is also the default implementation of Spring

A User entity class is given here

package com.zhiying.pojo;

public class User {
    private String name;

    public User(a) {
        System.out.println("Parametric structure");
    }

    public String getName(a) {
        return name;
    }

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

    public void show(a) {
        System.out.println("name="+ name); }}Copy the code

The entity class is then configured


      
<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="user" class="com.zhiying.pojo.User">
        <property name="name" value="He Zhiying"/>
    </bean>
</beans>
Copy the code

Let’s test it out

import com.zhiying.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        User user = (User) context.getBean("user");
        user.show();
        User user1 = (User) context.getBean("user"); System.out.println(user == user1); }}Copy the code

Results:

As you can see, we don’t have a new object, but the no-parameter construct is called, which means our Spring call, which means we’ve created the object for us, and we’re taking it at will, and you can see that we’re taking the same object. When the configuration file is loaded, container-managed objects are initialized

There are three ways to construct with parameters, so let’s look at the first one

package com.zhiying.pojo;

public class User {
    private String name;

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

    public String getName(a) {
        return name;
    }

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

    public void show(a) {
        System.out.println("name="+ name); }}Copy the code

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

<! This is a no-argument construction implementation -->
<! -- <bean id="user" class="com.zhiying.pojo.User">-->
<! -- <property name="name" value=" "/>-->
<! -- </bean>-->
<! -- parameter construction, directly controlled by the parameter name, most commonly used -->
    <bean id="user" class="com.zhiying.pojo.User">
        <constructor-arg name="name" value="He Zhiying"/>
    </bean>
</beans>
Copy the code
import com.zhiying.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        User user = (User) context.getBean("user");
        user.show();
        User user1 = (User) context.getBean("user"); System.out.println(user == user1); }}Copy the code

Use constructor-arg instead of property in the configuration file

Second, change the configuration file on the basis of the first


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

<! This is a no-argument construction implementation -->
<! -- <bean id="user" class="com.zhiying.pojo.User">-->
<! -- <property name="name" value=" "/>-->
<! -- </bean>-->

<! -- parameter construction, directly controlled by the parameter name, most commonly used -->
<! -- <bean id="user" class="com.zhiying.pojo.User">-->
<! -- <constructor-arg name="name" value=" name" />
<! -- </bean>-->

<! -- Parameter constructs are created by type, not commonly used -->
        <bean id="user" class="com.zhiying.pojo.User">
            <constructor-arg type="java.lang.String" value="He Zhiying"/>
        </bean>
</beans>
Copy the code

You can also run it

Third, on the basis of the second configuration file can be changed


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

<! This is a no-argument construction implementation -->
<! -- <bean id="user" class="com.zhiying.pojo.User">-->
<! -- <property name="name" value=" "/>-->
<! -- </bean>-->

<! -- parameter construction, directly controlled by the parameter name, most commonly used -->
<! -- <bean id="user" class="com.zhiying.pojo.User">-->
<! -- <constructor-arg name="name" value=" name" />
<! -- </bean>-->

<! -- Parameter constructs are created by type, not commonly used -->
<! -- <bean id="user" class="com.zhiying.pojo.User">-->
<! -- <constructor-arg type="java.lang.String" value=" hezhingeng "/>
<! -- </bean>-->
<! -- Parameter construct via subscript assignment -->
    <bean id="user" class="com.zhiying.pojo.User">
        <constructor-arg index="0" value="He Zhiying"/>
    </bean>
</beans>
Copy the code

So you can do that, so those are the three parameterized constructors that create objects