Spring factory method injection properties

1 static factory injection

In Spring, you can also use static factories to instantiate Beans. This method needs to provide a static factory method to create an instance of the Bean.
① Create entity class

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

② Create a static factory class
Create a class named MyBeanFactory and create a static method named createBean() in this class to create instances of Bean, as shown below.

public class MyBeanFactory {
    
    
    // 创建Bean实例的静态工厂方法
    public static Person createBean() {
    
    
    	Person person = new Person();
    	person.setName("glp");
        return person;
    }
}

③ Create Spring configuration file
Create Spring configuration file applicationContext.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" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
    
    <bean id="person" class="com.mengma.instance.static_factory.MyBeanFactory"
        factory-method="createBean" />
        
</beans>

In the above code, a bean with an id of person is defined,The class attribute specifies the 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.

2 instance factory injection

In Spring, another way to instantiate Bean is to use an instance factory to directly create Bean instances in member methods.

At the same time, in the configuration file, the bean that needs to be instantiated does not directly point to the 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){
    
    
		this.name = name;
    }
}

② Create an instance factory class
Create a class named MyBeanFactory.

public class MyBeanFactory {
    
    
    public MyBeanFactory() {
    
    
        System.out.println("person3工厂实例化中");
    }
    // 创建Bean的方法
    public Person createBean() {
    
    
        Person person = new Person();
    	person.setName("cbj");
        return person;
    }
}

③ Create Spring configuration file
Create Spring configuration file applicationContext.xml as shown below.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
    
    <!-- 配置实例工厂 -->
    <bean id="myBeanFactory" class="com.mengma.instance.factory.MyBeanFactory" />
    
    <!-- factory-bean属性指定一个实例工厂,factory-method属性确定使用工厂中的哪个方法 -->
    
    <bean id="person" factory-bean="myBeanFactory" factory-method="createBean" />
    
</beans>

In the above code,First configure an instance factory bean, and then configure the bean that needs to be instantiated. The id of the person Bean, a factory-beanproperty instance to specify a factory, the attribute value is the id attribute value factory instance. Use the factory-method attribute to determine the use of the createBean() method in the factory.

Guess you like

Origin blog.csdn.net/glpghz/article/details/110304453