Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Look up keyword or section
Like this

Table Of Contents

0 of .
Results for:
No results containing your search query
P. 1


Ratings: (0)|Views: 356|Likes:

More info:

Published by: Chandra Siva Prasad Achanta on Jul 04, 2011
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





Chapter 3. Beans, BeanFactory and the ApplicationContext3.1. Introduction
Two of the most fundamental and important packages in Spring arethe
packages. Code inthese packages provides the basis for Spring's
 Inversion of Control
 Dependency Injection
) features. The 
 provides an advancedconfiguration mechanism capable of managing beans (objects) of any nature, usingpotentially any kind of storage facility. The ApplicationContext builds on top of the BeanFactory (it's a subclass) and adds other functionality such as easier integrationwith Springs AOP features, message resource handling (for use ininternationalization), event propagation, declarative mechanisms to create theApplicationContext and optional parent contexts, and application-layer specificcontexts such as the
, among other enhancements.In short, the
provides the configuration framework and basicfunctionality, while the
adds enhanced capabilities to it, some of them perhaps more J2EE and enterprise-centric. In general, an ApplicationContext isa complete superset of a BeanFactory, and any description of BeanFactory capabilitiesand behavior should be considered to apply to ApplicationContexts as well.Users are sometimes unsure whether a BeanFactory or an ApplicationContext are bestsuited for use in a particular situation. Normally when building most applications in aJ2EE-environment,
the best option is to use the ApplicationContext 
, since it offers allthe features of the BeanFactory and adds on to it in terms of features, while alsoallowing a more declarative approach to use of some functionality, which is generallydesirable. The main usage scenario when you might prefer to use the BeanFactory iswhen memory usage is the greatest concern (such as in an applet where every lastkilobyte counts), and you don't need all the features of the ApplicationContext.This chapter covers material related to both the BeanFactory and theApplicationContext. When mention is made only of the BeanFactory, you may alwaysassume the text also applies to the ApplicationContext. When functionality is onlyavailable in the ApplicationContext, explicit mention is made of this.
3.2. BeanFactory and BeanDefinitions - the basics
3.2.1. The BeanFactory
 is the actual
which instantiates, configures, and manages anumber of beans. These beans typically collaborate with one another, and thus havedependencies between themselves. These dependencies are reflected in theconfiguration data used by the BeanFactory (although some dependencies may not bevisible as configuration data, but rather be a function of programmatic interactionsbetween beans at runtime).A BeanFactory is represented by theinterface
, for which there aremultiple implementations. The most commonly used simple BeanFactoryimplementation is
. (Thisshould be qualified with the reminder that ApplicationContexts are a subclass of BeanFactory, and most users end up using XML variants of ApplicationContext).Although for most scenarios, almost all user code managed by the BeanFactory doesnot have to be aware of the BeanFactory, the BeanFactory does have to be instantiatedsomehow. This can happen via explicit user code such as:
Resource res = new FileSystemResource("beans.xml");XmlBeanFactory factory = new XmlBeanFactory(res);
ClassPathResource res = new ClassPathResource("beans.xml");XmlBeanFactory factory = new XmlBeanFactory(res);
ClassPathXmlApplicationContext appContext = newClassPathXmlApplicationContext(new String[] {"applicationContext.xml", "applicationContext-part2.xml"});// of course, an ApplicationContext is just a BeanFactoryBeanFactory factory = (BeanFactory) appContext;
Note: once you have learned the basics about bean factories and applicaiton contexts,from this chapter, it will also be useful to learn about Spring's
abstraction, asdescribed in Chapter 4,
.The location pathor paths supplied to an ApplicationContext constructor are actually resource strings,
and in simple form are treated appropriately to the specific context implementation(i.e. ClassPathXmlApplicationContext treats a simple location path as a classpathlocation), but may also be used with special prefixes to force loading of definitionsfrom the classpath or a URL, regardless of the actual context type. Another specialprefix,
, allows all context definiton files of the same name on theclasspath to be found and combined to build a context. Please see the chapterreferenced above for much more information on the topic of 
s.For many usage scenarios, user code will not have to instantiate the BeanFactory orApplicationContext, since Spring Framework code will do it. For example, the weblayer provides support code to load a Spring ApplicationContext automatically as partof the normal startup process of a J2EE web-app. This declarative process isdescribed here:  While programmatic manipulation of BeanFactories will be described later, thefollowing sections will concentrate on describing the configuration of BeanFactories.A BeanFactory configuration consists of, at its most basic level, definitions of one ormore beans that the BeanFactory must manage. In an XmlBeanFactory, these areconfigured as one or more
elements inside a top-level
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd"><beans><bean id="..." class="...">...</bean><bean id="..." class="...">...</bean>...</beans>
3.2.2. The BeanDefinition
Bean definitions inside a DefaultListableBeanFactory variant (like XmlBeanFactory)are represented as BeanDefinition objects, which contain (among other information)the following details:
a class name: this is normally the actual implementation class of the bean beingdescribed in the bean definition. However, if the bean is to be constructed by

Activity (2)

You've already reviewed this. Edit your review.
1 thousand reads
1 hundred reads

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->