Three methods of creating objects in Spring

Keywords: Spring

As we all know, the main feature of Spring is that it will no longer use the traditional method of creating objects, but put the steps of creating objects into the xml configuration file. Then, let's summarize three methods of configuring instances through xml: common constructor creation (no parameter constructor), static factory method creation, and instance factory method creation

Normal construction method creation (no parameter construction method)

This method is created by using the parameterless construction method in a class. For example, we have the class Hello:

public class Hello {
    private String name;
    public Hello(){
    //This method is a parameterless construction method. If it is not written, the system will create it by default,
    //However, if a parametric construction method is written, the method will not be created by the system by default

    public String getName() {
        return name;

    public void setName(String name) { = name;
    public void show(){

Then we configure a corresponding bean node in the xml file to obtain the instance of this class, as follows:

The value of the created object name is id, and the function of the property tag is to assign a value to the member variable in the Hello class,

Here, the value of the name attribute is assigned to xyx_spring

<bean id="hello" class="com.xyx.pojo.Hello">
        <property name="name" value="xyx_spring"/>

Then we write the test file:

    //Get the context object of Spring! Generally speaking, it is to put the xml configuration file into the object context
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

    //Find the bean tag with id hello in the context (actually in beans.xml) and assign it to a variable of type hello
Hello hello = (Hello) context.getBean("hello");
    //Call the show method in hello;

Output results:

  Static factory method creation

In addition to the normal construction method, we can also use the static factory method to create a bean, as follows:

public class Hello2 {
    public void show(){
        System.out.println("Hello_Using the static factory method+spring create object");

We create a factory for the previous class:

public class Hello_2Factory {
    public static Hello2 getBean() {//Static factory method - the static representation method is modified by static, and the factory represents that the return value is an object
        System.out.println("Create by factory Hello2 Object of");
        return new Hello2();

  In the factory method, the returned value is an instance of Hello, so we only need to call the factory method (getBean method) in this class to implement the static factory method to create bean s, so we write the following in the xml configuration file:

<bean id="hello2" class="com.xyx.pojo.Hello_2Factory" factory-method="getBean"/>

Here we can see that the created object is the object of the factory class, but a factory method attribute is added, and the declared value of this attribute is the factory method, that is, the factory method will be called.

The test classes are as follows:

 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//It should be noted here that we do not need to deliberately write and call construction methods,
//When an object is created, the method declared by factory method is automatically called and the object of another class is returned.
 Hello2 hello2 = (Hello2) context.getBean("hello2");;

Operation results:

  Example factory method

The code is as follows:

Entity class:

public class Hello3 {
    public void show(){
        System.out.println("Hello_Using common factory methods+spring create object");

Factory type:

public class Hello_3Factory {

//The method in the instance factory method is not decorated with static
    public Hello3 getBean3() {
        System.out.println("Create through normal factory Hello3 Object of");
        return new Hello3();

When the factory method is not static, what should our xml file be like

<!--first bean The factory method object is created,-->
<bean id="hello_3Factory3" class="com.xyx.pojo.Hello_3Factory"/>

<!--the second bean Created object id by hello3,
factory-method Indicates that the factory-bean Object getBean3 Method, by getBean3 Method to create a get hello3 Examples of
<bean id="hello3" factory-bean="hello_3Factory3" factory-method="getBean3"/>

Test code:

 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Hello3 hello3 = (Hello3) context.getBean("hello3");;

Operation results:


The third example factory method is different from the second static factory method in two main points

1. First, the factory method is static, while the instance factory method is dynamic

2. The static factory in the configuration file only needs to be implemented by a bean tag, in which the static method to be called is written, and the return value of the method is the object of the entity class. The instance factory needs two bean tags in the configuration file. The factory bean in the second tag refers to the id of the first bean, and the factory method is the method name. The return value of the method must also be the object of the instance class.

Posted by spighita on Sun, 19 Sep 2021 03:51:59 -0700