Introduction:
In this article, we focus on the configuration file beans.xml for IoC purpose.
1. A simple bean injected with simple property
1) Here we declare a simple bean whose name is student and type is edu.xmu.domain.Student.
2) This instance has a property named id whose value we set as 1.
2. A simpe bean injected with constructor.
1) The constructor-arg has to be ordered by declaration in constructor. As there is no name to identify the arg. The only reason to assign the value to a property is its type.
3. A simple bean injected with complex property.
1) By refering to another bean.
4. A simple bean injected with complex constructor
1) Pay attention to the two different approaches in constructing instance
(1) Assign as a property "ref"
(2) Assign as a sub-element with tag
5. A simple bean injected with complex constructor using Factory method
public Student(Integer id, String name, Integer age, Address address, Address homeAddress) { this.id = id; this.name = name; this.age = age; this.address = address; this.homeAddress = homeAddress; } public static Student createInstance(Integer id, String name, Integer age, Address address, Address homeAddress) { System.out.println("createInstance invoked"); return new Student(id, name, age, address, homeAddress); }
1) What's the purpose of using factory-method instead of simply using constructor?
(1) The only benefit of using factory-method is return type can be sub-class of declared return type. But what's the point?
6. Refer to other beans
1) Using tag for the scope of whole xml including parent xml and children xml
2) Using tag for the scope of only current xml
3) Using tag for the scope of only parent xml