Three methods of instantiating beans

Keywords: Java Spring

preface

The core of the Spring framework is the concept of Bean. Here we will talk about three methods to create Bean instances.

At present, there are three methods to create beans.
(1) Configuring beans through XML
(2) Declare beans through annotations
(3) Configuring beans through JavaConfig

XML configuration Bean

Disadvantages:
(1) XML configuration is cumbersome.
(2) Configuration errors cannot be found at compile time.

Implementation method:
(1) Declaration class
(2) Define the Bean instance corresponding to the class through the XML configuration file
(3) spring scans the corresponding XML configuration file to create a Bean instance

Step 1: declare classes

//Declaration class
public class Rabbit {
    private String eyes;
    private String legs;

    public Rabbit(String eyes, String legs) {
        this.eyes = eyes;
        this.legs = legs;
    }

    public String getEyes() {
        return eyes;
    }

    public void setEyes(String eyes) {
        this.eyes = eyes;
    }

    public String getLegs() {
        return legs;
    }

    public void setLegs(String legs) {
        this.legs = legs;
    }
}

Step 2: define the Bean instance corresponding to the class through the XML configuration file

(1) Create a Bean by calling the constructor in the class.

Because there will be parameterless constructors and parameterless constructors in the class. Therefore, there are two ways to create beans through constructors. One is to call the parameterless constructor to create a Bean, and the other is to call the parameterless constructor to create a Bean.

Method 1: call the parameterized constructor to create a Bean

//Call the constructor with parameters and declare the Bean
<bean id="rabbit" class="com.test.domain.Rabbit">
	<constructor-arg name="eyes" value="Rabbit eyes"/>
	<constructor-arg name="legs" value="Rabbit retreat"/>
</bean>

As shown above, id is used to uniquely determine a Bean. Class refers to the location of the corresponding class in the project, name refers to the name of the field in the class, and value refers to the value to be assigned to the corresponding field.

Method 2: call the parameterless constructor to create a Bean

//Call the parameterless constructor and declare the Bean
<bean id="rabbit" class="com.test.domain.Rabbit">
</bean>

As shown above, a Bean is declared.

Create beans based on annotations

Implementation steps:
(1) Class
(2) spring scans the corresponding class file to create a Bean instance

Examples are as follows:

//Declaration class
//adding annotations 
@Resource
public class Rabbit {
    private String eyes;
    private String legs;

    public Rabbit(String eyes, String legs) {
        this.eyes = eyes;
        this.legs = legs;
    }

    public String getEyes() {
        return eyes;
    }

    public void setEyes(String eyes) {
        this.eyes = eyes;
    }

    public String getLegs() {
        return legs;
    }

    public void setLegs(String legs) {
        this.legs = legs;
    }
}

Then spring will scan the class according to the. After seeing the annotation, it will automatically generate the corresponding Bean.

JavaConfig configuration Bean

realization:
(1) Declare the corresponding method to create the Bean. Where BeanId is the name of the method.
(2) When spring scans the @ Bean method, it will create the corresponding Bean according to the method.

//Declaration class
public class Rabbit {
    private String eyes;
    private String legs;

    public Rabbit(String eyes, String legs) {
        this.eyes = eyes;
        this.legs = legs;
    }

    public String getEyes() {
        return eyes;
    }

    public void setEyes(String eyes) {
        this.eyes = eyes;
    }

    public String getLegs() {
        return legs;
    }

    public void setLegs(String legs) {
        this.legs = legs;
    }
}

//Create the corresponding Bean through the @ Bean annotation
@Bean
public Rabbit rabbit(){
	Rabbit rabbit = new Rabbit();
	return userDao;
}

Note that there is a misunderstanding here. Here, beans are configured through JavaConfig. Although the new keyword is used to create a Bean, when to call this method to instance an object is still controlled by the IOC container, so it still conforms to the concept of IOC control inversion.

Posted by surfinglight on Thu, 30 Sep 2021 15:46:59 -0700