Spring note 5 (using hierarchical Bean Factory)

Source: Internet
Author: User
This article studies the usage of spring layered Bean Factory, first look at the relevant code:

1 parent. xml
2
3 <! Doctype beans public "-// spring // DTD bean // en"
Http://www.springframework.org/dtd/spring-beans.dtd>
4 <beans>
5 <bean id = "injectbean" class = "Java. Lang. String">
6 <constructor-Arg>
7 <value> bean in parent </value>
8 </constructor-Arg>
9 </bean>
10 <bean id = "injectbeanparent" class = "Java. Lang. String">
11 <constructor-Arg>
12 <value> bean in parent </value>
13 </constructor-Arg>
14 </bean>
15 </beans>

1 beans. xml
2
3 <! Doctype beans public "-// spring // DTD bean // en"
Http://www.springframework.org/dtd/spring-beans.dtd>
4 <beans>
5 <! -- Hierarchical bean factories -->
6 <bean id = "target1" class = "simpletarget">
7 <property name = "Val">
8 <ref bean = "injectbeanparent"/>
9 </property>
10 </bean>
11
12 <bean id = "TARGET2" class = "simpletarget">
13 <property name = "Val">
14 <ref local = "injectbean"/>
15 </property>
16 </bean>
17
18 <bean id = "target3" class = "simpletarget">
19 <property name = "Val">
20 <ref parent = "injectbean"/>
21 </property>
22 </bean>
23
24 <bean id = "injectbean" class = "Java. Lang. String">
25 <constructor-Arg>
26 <value> bean in child </value>
27 </constructor-Arg>
28 </bean>
29 </beans>
30

Simpletarget has only one attribute, string Val.

1 public static void main (string [] ARGs ){
2 beanfactory parent = new xmlbeanfactory (New filesystemresource (
3 "build/parent. xml "));
4 beanfactory child = new xmlbeanfactory (New filesystemresource (
5 "build/beans. xml"), parent );
6
7 simpletarget target1 = (simpletarget) child. getbean ("target1 ");
8 simpletarget TARGET2 = (simpletarget) child. getbean ("TARGET2 ");
9 simpletarget target3 = (simpletarget) child. getbean ("target3 ");
10
11 system. Out. println (target1.getval ());
12 system. Out. println (target2.getval ());
13 system. Out. println (target3.getval ());
14}

Running result:
Bean in parent
Bean in child
Bean in parent

Analysis process:
When Bean Factory is loaded, Bean Factory at each layer is loaded, beanfactory parent = new xmlbeanfactory (New filesystemresource ("build/parent. xml "));
Beanfactory child = new xmlbeanfactory (New filesystemresource ("build/beans. xml"), parent );
The parent parameter specifies the parent-child relationship between Bean factory.

Analysis result:
The result of the first line is a parameter defined in the parent factory. It indicates that the parent fanwe element can be directly called in the Child fanwe, similar to the inheritance relationship in Java.
The result of the second line indicates that the local in <ref local = "injectbean"/> points to the fantory element.
The result in the third row is bean in parent, indicating that the parent points to the element in the parent fanintent in <ref parent = "injectbean"/>.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.