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.
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:
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 + "]"; } }
XML configuration:
<?xml version="1.0" encoding="UTF-8"?><beans><bean><property></property><property></property></bean> </beans>
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.
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.
Person class:
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 + "]"; } }
XML configuration:
<?xml version="1.0" encoding="UTF-8"?><beans><bean><constructor-arg></constructor-arg><constructor-arg></constructor-arg></bean> </beans>
The static factory object is created when the container is loaded.
<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>
@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]}
@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]}
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!