Home > Java > javaTutorial > body text

Detailed explanation of how Spring creates objects

零下一度
Release: 2017-07-21 22:09:02
Original
2294 people have browsed it

For information on the construction of Spring, please refer to: A brief analysis of the construction of the Spring framework. Before testing, you should configure the environment and import the relevant Jar packages. Objects created by Spring are in singleton mode by default unless specified through scope.

1. Create objects through constructors.

2.1 Use the no-argument constructor + setter method to inject values ​​

The most basic way to create an object is to have a no-argument constructor (no constructor is written in the class, the default is There is a constructor. If any constructor is written, the default no-argument constructor will not be automatically created!!) and the setter method of the field.

Person class:

Detailed explanation of how Spring creates objects
package com.mc.base.learn.spring.bean;public class Person {private String name;private Integer id;    public String getName() {return name;
    }public void setName(String name) {this.name = name;
    }public Integer getId() {return id;
    }public void setId(Integer id) {this.id = id;
    }

    @Overridepublic String toString() {return "Person [name=" + name + ", id=" + id + "]";
    }

}
Copy after login
Detailed explanation of how Spring creates objects

XML configuration:

Detailed explanation of how Spring creates objects
<?xml  version="1.0" encoding="UTF-8"?><beans><bean><property></property><property></property></bean>    
</beans>
Copy after login
Detailed explanation of how Spring creates objects

its essence Create an object for:

SpringContext using the parameterless constructor, and then assign the value using the setter method. Therefore, if the parameterless constructor does not exist, an error will be reported when the Spring context creates the object.

Detailed explanation of how Spring creates objects
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'person' defined in class path resource [applicationContext.xml]: Instantiation of bean failed; 
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mc.base.learn.spring.bean.Person]: No default constructor found; 
nested exception is java.lang.NoSuchMethodException: com.mc.base.learn.spring.bean.Person.
Copy after login
Detailed explanation of how Spring creates objects

2.2 Use the parameterized constructor directly Inject the

Person class:

Detailed explanation of how Spring creates objects
package com.mc.base.learn.spring.bean;public class Person {private String name;private Integer id;    public Person(String name, Integer id) {super();this.name = name;this.id = id;
    }public String getName() {return name;
    }public void setName(String name) {this.name = name;
    }public Integer getId() {return id;
    }public void setId(Integer id) {this.id = id;
    }

    @Overridepublic String toString() {return "Person [name=" + name + ", id=" + id + "]";
    }

}
Copy after login
Detailed explanation of how Spring creates objects

XML configuration:

Detailed explanation of how Spring creates objects
<?xml  version="1.0" encoding="UTF-8"?><beans><bean><constructor-arg></constructor-arg><constructor-arg></constructor-arg></bean>    
</beans>
Copy after login
Detailed explanation of how Spring creates objects

二, create objects through static factory methods.

The static factory object is created when the container is loaded.

Detailed explanation of how Spring creates objects##
package com.mc.base.learn.spring.factory;import com.mc.base.learn.spring.bean.Person;public class PersonStaticFactory {    public static Person createPerson(){return new Person();
    }    /** * 工厂方法带有参数如何处理?
     * @Title: createPerson 
     * @Description: TODO(这里用一句话描述这个方法的作用) 
     * @param  @param id
     * @param  @param name
     * @param  @return 
     * @return Person    返回类型 
     * @throws     */public static Person createPerson(Integer id,String name){return new Person(name,id);
    }
}
Copy after login
Detailed explanation of how Spring creates objects
##
    <span   style="max-width:90%"><!--<span style="color: #008000;">静态的工厂方法核心是class+factory-method <span style="color: #008000;">--><span style="color: #0000ff;"><span style="color: #800000;"><span style="color: #ff0000;"><span style="color: #0000ff;"><span style="color: #ff0000;"><span style="color: #0000ff;"><span style="color: #ff0000;"><span style="color: #0000ff;"><span style="color: #0000ff;"><span style="color: #800000;"><span style="color: #0000ff;"><br>    <span style="color: #008000;"><span style="color: #008000;"><span style="color: #008000;"> <span style="color: #0000ff;">bean <span style="color: #ff0000;">id<span style="color: #0000ff;">="person"<span style="color: #ff0000;"> class<span style="color: #0000ff;">="com.mc.base.learn.spring.factory.PersonStaticFactory"<span style="color: #ff0000;"> factory-method<span style="color: #0000ff;">="createPerson"<span style="color: #0000ff;">><br><span style="color: #008000;"><!--<span style="color: #008000;">通过property方法向createPerson传递参数 <span style="color: #008000;">--></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br>        <property name="name" value="LiuChunfu"></property><br>        <property name="id" value="125"></property><br>    <br></span></span></span></span></span></span></span></span></span>
Copy after login
The test is as follows:

    @Testpublic void testName() throws Exception {
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person=ac.getBean("person3", Person.class);
        System.out.println(person);//Person [name=LiuChunfu, id=125]}
Copy after login
3. Create objects through instance factories.

The instance factory is to call objects through instances, but the objects obtained are ultimately in single-interest mode. The objects created by the instance factory and the static factory are both singleton mode. The difference between the two is the actual difference in creating the object. The static factory is created when the container is created, and the instance factory is created when the method is called. Readers who know the singleton pattern design (hungry man style and lazy man style) in Java design patterns must have some experience of the static factory pattern and instance factory pattern here.

##
package com.mc.base.learn.spring.factory;import com.mc.base.learn.spring.bean.Person;public class PersonFactory {public Person createInstance() {return new Person();
    }
}
Copy after login
Detailed explanation of how Spring creates objects
Detailed explanation of how Spring creates objects##
    <bean></bean><bean><property></property><property></property></bean>
Copy after login
    @Testpublic void testName() throws Exception {
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person=ac.getBean("person4",Person.class);
        System.out.println(person);//Person [name=LiuChunfu, id=125]}
Copy after login
Of course, when creating an object and passing parameters above, in addition to passing in the default parameters when creating the object, you can also pass parameters later through the setter method.

The above is the detailed content of Detailed explanation of how Spring creates objects. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!