Java Reference
In-Depth Information
the control has been removed from ClassA and kept elsewhere (that is, in an XML configuration file,
illustrated in Listing 5-3) and thus “inverted” by dependency injection (DI) because the dependency
is delegated to an external system, in other words, the configuration metadata. Listing 5-3 illustrates
a typical Spring configuration metadata file.
Listing 5-3. Configuration Metadata
1. <?xml version="1.0" encoding="UTF-8"?>
2. <beans xmlns=" http://www.springframework.org/schema/beans "
3. xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "
4. xsi:schemaLocation=" http://www.springframework.org/schema/beans
5. http://www.springframework.org/schema/beans/spring-beans-3.2.xsd " >
6.
7.
8. <bean id=" .." class=".."/>
9.
10. <!-- more bean definitions -- >
11.
12. </beans>
Line 2 : To use tags such as <beans> , <bean> , and so on, certain namespaces
need to be declared. The core Spring Framework comes with ten configuration
namespaces. For now let's concentrate on the beans namespace; as the
chapter progresses, other namespaces will be used. However, in this topic we
require only the aop , beans , and context schemas, so if you are interested in
using others, you can find them at http://static.springsource.org/spring/
docs/3.2.2.RELEASE/spring-framework-reference/html/xsd-config.html .
Line 2 : When describing beans in an XML-based configuration, the root element
of the XML file is <beans> from Spring's bean schema. The entire Spring
configuration, including <bean> declarations, is placed in the top-level <beans> .
Line 8 : <bean> is the most basic configuration unit in Spring. It tells Spring to
create an object for the application. These <bean> definitions correspond to the
actual objects that make up the application.
Line 8 : The id attribute is a string that helps identify the individual bean
definition. The class attribute defines the type of the bean and uses the fully
qualified class name. The value of the id attribute refers to the collaborating
objects.
Note While XML is a classic way for defining configuration metadata, you can use annotations (from
Spring 2.5 and newer) or Java code (from Spring 3.0 and newer).
Listing 5-4 illustrates the bean definition that should be included in the configuration metadata file
illustrated in Listing 5-3 to inject the classB dependency in Listing 5-2.
 
 
Search WWH ::




Custom Search