SoFunction
Updated on 2025-04-14

About Spring factory method injecting attributes

Static factory injection

In Spring, you can also instantiate beans using static factories. This approach requires providing a static factory method to create an instance of the bean.

① Create entity class

public class Person{
	String name;
	public void setName(String name){
		 = name;
    }
}

② Create a static factory class

Create a class called MyBeanFactory and create a static method called createBean() in the class to create instances of the bean as shown below.

public class MyBeanFactory {
    // Static factory method for creating bean instance    public static Person createBean() {
    	Person person = new Person();
    	("glp");
        return person;
    }
}

③ Create Spring Configuration File

Create Spring's configuration file.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
    xmlns:xsi="http:///2001/XMLSchema-instance" xmlns:p="/schema/p"
    xsi:schemaLocation="/schema/beans
    /schema/beans/spring-beans-3.">
    
    <bean  class=".static_factory.MyBeanFactory"
        factory-method="createBean" />
        
</beans>

In the above code, a bean with id as person is defined, where the class attribute specifies its corresponding factory implementation class as MyBeanFactory, and the factory-method attribute is used to tell the Spring container to call the createBean() method in the factory class to obtain an instance of the bean.

Instance factory injection

In Spring, there is another way to instantiate a bean, which is to use an instance factory to create an instance of a bean directly in a member method.

At the same time, in the configuration file, the bean that needs to be instantiated does not directly point to its instantiated class through the class attribute, but configures an instance factory through the factory-bean attribute, and then uses the factory-method attribute to determine which method in the factory to use.

① Create entity class

public class Person{
	String name;
	public void setName(String name){
		 = name;
    }
}

② Create an instance factory class

Create a class called MyBeanFactory.

public class MyBeanFactory {
    public MyBeanFactory() {
        ("person3 factory instantiation");
    }
    // Method to create a bean    public Person createBean() {
        Person person = new Person();
    	("cbj");
        return person;
    }
}

③ Create Spring Configuration File

Create Spring's configuration file as shown below.

&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;beans xmlns="/schema/beans"
    xmlns:xsi="http:///2001/XMLSchema-instance" xmlns:p="/schema/p"
    xsi:schemaLocation="/schema/beans
    /schema/beans/spring-beans-3."&gt;
    
    &lt;!-- Configure the instance factory --&gt;
    &lt;bean  class="" /&gt;
    
    &lt;!-- factory-beanProperties specify an instance factory,factory-methodAttributes determine which method in the factory is used --&gt;
    
    &lt;bean  factory-bean="myBeanFactory" factory-method="createBean" /&gt;
    
&lt;/beans&gt;

In the above code, first configure an instance factory bean, and then configure the bean that needs to be instantiated.

In a bean with id is person, usefactory-beanThe attribute specifies an instance factory, and the attribute value is the id attribute value of the instance factory.

Use the factory-method property to determine the use of the createBean() method in the factory.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.