Person class (with or without parametric construction method):
package com.li.spring; public class Person { private String name; private int age; public Person(){ System.out.println("Initial constructor"); } public void setName(String name) { System.out.println("Settings properties were invoked"); this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void hello(){ System.out.println("Hello: " + name); } @Override public String toString() { return "HelloWorld [name=" + name + ", age=" + age + "]"; } }
1. Creation of constructors
1.1 Through a parametric constructor (default)
<bean id="person" class="com.li.spring.Person" ></bean>
1.2 Through a parametric constructor (you need to add a parametric constructor to Person and assign values to attributes)
<bean id="person" class="com.li.spring.Person" > <constructor-arg index="0" value="someone"></constructor-arg> <constructor-arg index="1" value="20"></constructor-arg> </bean>
2. Create through factory mode
package com.li.spring; public class PersonFactory { public Person createPerson(){ return new Person(); } }
xml:
<bean id="personFactory" class="com.li.spring.PersonFactory"></bean> <bean id="person" factory-bean="personFactory" factory-method="createPerson"></bean>
3. Create using static factories (add static directly on the basis of factories, do not need to create objects, use it directly)
package com.li.spring; public class PersonFactory { public static Person createPerson(){ return new Person(); } }
xml:
<bean id="person" class="com.li.spring.PersonFactory" factory-method="createPerson"></bean>
Please give me some advice on the inappropriateness.