In your /classes directory (which is at the same level as /src), create a folder called "spring-beans".
In this folder, create a file bean-cart.xml, like this:
<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-2.5.xsd">
<bean id="cart" class="org.confucius.Cart" />
</beans>
Here we have declared a Spring Bean with the id "cart" as an instance of class org.confucius.Cart.
In /classes/spring-beans, create a file bean-shop.xml, like this:
<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-2.5.xsd">
<bean id="shop" class="org.confucius.Shop">
<property name="cart" ref="cart"/>
</bean>
</beans>
Here we have declared a Spring Bean with id 'shop' as an instance of class org.confucius.Shop.
Further, we have asked Spring to inject the 'cart' bean into the 'cart' property of this (shop) bean.
Next, in your /classes/spring-beans, create a file bean-checkout.xml, like this:
<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-2.5.xsd">
<bean id="checkout" class="org.confucius.Checkout">
<property name="cart" ref="cart"/>
</bean>
</beans>
Here we have declared a Spring Bean with id 'checkout' as an instance of class org.confucius.Checkout. Then we have injected the 'cart' bean into the 'cart' property of this (checkout) bean.
This completes the Spring Bean declaration.
For convenience of loading (you will see later), let us pack these bean declarations in a single parent xml.
In your /classes folder, create a file spring-beans.xml, like this:
(Note that we are creating this in the /classes folder and not in /classes/spring-beans folder - because we need this parent xml in the Classpath)
<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-2.5.xsd">
<import resource="spring-beans/bean-cart.xml" />
<import resource="spring-beans/bean-shop.xml" />
<import resource="spring-beans/bean-checkout.xml" />
</beans>
By loading this single XML, our program will essentially be loading all the individual bean declarations.
No comments:
Post a Comment