Spring IoC container creation object_study notes

The methods for creating objects in the spring container are: constructor method, static factory, and dynamic (instance) factory;

One, the constructor

1. No-parameter constructor

<?xml version="1.0" encoding="UTF-8"?>
<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
         http://www.springframework.org/schema/beans/spring-beans.xsd">

<!--配置我们的Dog类  spring 框架在底层 
  通过反射的机制  执行了我们的构造方法-->
<bean id="dog" class="cn.pb.dao.impl.Dog"></bean>
</beans>

Configuring a bean directly in this way is equivalent to calling the parameterless constructor of the Dog class. If the parameterless constructor is not present, an error will be reported when the Spring context creates the object.

2. No-argument constructor plus setter method to inject the value of the field

Prerequisite: The class needs to have a parameterless constructor and a set method for properties.
In fact, use the property tag to set the name and value under the bean tag

<?xml version="1.0" encoding="UTF-8"?>
<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 
    http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean class="com.mc.base.learn.spring.bean.Person" id="person">
        <property name="name" value="LiuChunfu"></property>
        <property name="id" value="125"></property>
    </bean>    
</beans>

If a field is a reference type, no value is used, and ref = "xxx". xxx is the id or name of a bean;

3. Parametric constructor

Just below the bean tag, there is a tag to determine the parameters of the bean's constructor.

<?xml version="1.0" encoding="UTF-8"?>
<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 
    http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean class="com.mc.base.learn.spring.bean.Person" id="person">
        <constructor-arg name="id" value="123"></constructor-arg>
        <constructor-arg name="name" value="LiuChunfu"></constructor-arg>
    </bean>
    
</beans>

You can also refer to the Ioc injection method by means of parameter subscripts

Two, static factory

package cn.pb.dao;

import cn.pb.dao.impl.Dog;

/**
 * AnimalFactory静态工厂类
 */
public class AnimalFactory {
    
    
    /**
     * 可以看到程序没有走构造方法
     */
    public AnimalFactory(){
    
    
        System.out.println("静态工厂的无参构造====");
    }
//静态工厂,不会走无参构造
    public static Animal getDog(){
    
    
        System.out.println("工厂中静态获取Dog实例的方法");
        return new Dog();
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<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
         http://www.springframework.org/schema/beans/spring-beans.xsd">

   

    <!--02.通过静态工厂 来创建我们对象的实例
     工厂的本身构造不会执行 因为我们的方法是静态的 !类只会被加载,不会被实例化!
     getDog必须是静态的-->
    <bean id="dog" class="cn.pb.util.AnimalFactory" factory-method="getDog"></bean>  

</beans>

Three, dynamic (example) factory

package cn.pb.dao;

import cn.pb.dao.impl.Dog;

/**
 * AnimalFactory动态工厂类
 */
public class AnimalFactory {
    
    
    /**
     * 程序会先创建工厂实例 再调用getDog()方法
     */
    public AnimalFactory(){
    
    
        System.out.println("动态工厂的无参构造====");
    }
//动态工厂 会先走无参构造  创建实例
    public Animal getDog(){
    
    
        System.out.println("工厂中动态工厂获取Dog实例的方法");
        return new Dog();
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<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
         http://www.springframework.org/schema/beans/spring-beans.xsd">

   

    <!--03.动态工厂创建 对象的实例-->
    <bean id="factory" class="cn.pb.util.AnimalFactory"></bean><!-- 调用哪个工厂里的哪个方法  来创建对象  对象的id是dog-->
    <bean id="dog" factory-bean="factory" factory-method="getDog"/>

</beans>

Creating a Dog object requires the getDog() method in the AnimalFactory instance, so first create the AnimalFactory, and use the getDog method in the AnimalFactory instance (bean) to obtain the Dog instance object

Guess you like

Origin blog.csdn.net/qq_40084325/article/details/110990109