Introduction:
In this article, we focus on the configuration file beans.xml for IoC purpose.
1. A simple bean injected with simple property
<?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-3.0.xsd"> <bean id="student" class="edu.xmu.domain.Student"> <!-- collaborators and configuration for this bean go here --> <property name="id" value="1" /> </bean> </beans>
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.
<?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-3.0.xsd"> <bean id="student" class="edu.xmu.domain.Student"> <constructor-arg type="Integer" value="1" /> <constructor-arg type="String" value="Davy" /> <constructor-arg type="Integer" value="23" /> </bean> </beans>
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.
<?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-3.0.xsd"> <bean id="student" class="edu.xmu.domain.Student"> <property name="address" ref="address"></property> </bean> <bean id="address" class="edu.xmu.domain.Address"> <property name="country" value="China"></property> <property name="province" value="Shanghai"></property> <property name="city" value="Shanghai"></property> <property name="avenue" value="ChenhuiRD"></property> <property name="number" value="1000"></property> </bean> </beans>
4. A simple bean injected with complex 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-3.0.xsd"> <bean id="student" class="edu.xmu.domain.Student"> <constructor-arg type="Integer" value="1"></constructor-arg> <constructor-arg type="String" value="Davy"></constructor-arg> <constructor-arg type="Integer" value="23"></constructor-arg> <constructor-arg type="edu.xmu.domain.Address" ref="address1"></constructor-arg> <constructor-arg type="edu.xmu.domain.Address"> <ref bean="address2"/> </constructor-arg> </bean> <bean id="address1" class="edu.xmu.domain.Address"> <property name="country" value="China"></property> </bean> <bean id="address2" class="edu.xmu.domain.Address"> <property name="country" value="Canada"></property> </bean> </beans>
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 <ref>
5. A simple bean injected with complex constructor using Factory method
<?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-3.0.xsd"> <bean id="student" class="edu.xmu.domain.Student" factory-method="createInstance"> <constructor-arg type="Integer" value="1"></constructor-arg> <constructor-arg type="String" value="Davy"></constructor-arg> <constructor-arg type="Integer" value="23"></constructor-arg> <constructor-arg type="edu.xmu.domain.Address" ref="address1"></constructor-arg> <constructor-arg type="edu.xmu.domain.Address"> <ref bean="address2"/> </constructor-arg> </bean> <bean id="address1" class="edu.xmu.domain.Address"> <property name="country" value="China"></property> </bean> <bean id="address2" class="edu.xmu.domain.Address"> <property name="country" value="Canada"></property> </bean> </beans>
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
<?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-3.0.xsd"> <import resource="edu/xmu/service/service.xml"/> <import resource="edu/xmu/dao/dao.xml"/> </beans>
1) Using <ref bean="***"> tag for the scope of whole xml including parent xml and children xml
2) Using <ref local="***"> tag for the scope of only current xml
3) Using <ref parent="***"> tag for the scope of only parent xml