Global session: For portlet-based web applications. The global session scope beans
·
can be shared among all portlets within the same Spring MVC­powered portal
application.
Thread: A new bean instance will be created by Spring when requested by a new
·
thread, while for the same thread, the same bean instance will be returned. Note
that this scope is not registered by default.
Custom: Custom bean scope that can be created by implementing the interface
·
org.springframework.beans.factory.config.Scope and registering the custom
scope in Spring's configuration (for XML, use the class org.springframework.beans
.factory.config.CustomScopeConfigurer).
Resolving Dependencies
During normal operation, Spring is able to resolve dependencies by simply looking at your configuration
file or annotations in your classes. In this way, Spring can ensure that each bean is configured in the
correct order so that each bean has its dependencies correctly configured. If Spring did not perform this
and just created the beans and configured them in any order, a bean could be created and configured
before its dependencies. This is obviously not what you want and would cause all sorts of problems
within your application.
Unfortunately, Spring is not aware of any dependencies that exist between beans in your code. For
instance, take one bean, called bean A, which obtains an instance of another bean, called bean B, in the
constructor via a call to getBean(). For example, in the constructor of BeanA, you get an instance of
BeanB by calling ctx.getBean("beanB"), without asking Spring to inject the dependency for you. In this
case, Spring is unaware that bean A depends on bean B, and, as a result, it may instantiate bean A before
bean B. You can provide Spring with additional information about your bean dependencies using the
depends-on attribute of the <bean> tag. Listing 4-73 shows how the scenario for bean A and bean B would
be configured.
Listing 4-73. Manually Defining Dependencies
<bean id="beanA" class="com.apress.prospring.ch4.BeanA" depends-on="beanB"/>
<bean id="beanB" class="com.apress.prospring.ch4.BeanB"/>
In this configuration, we are asserting that bean beanA depends on bean beanB. Spring takes this into
consideration when instantiating the beans and ensures that beanB is created before beanA.
When developing your applications, avoid designing your applications to use this feature; instead,
define your dependencies by means of Setter and Constructor Injection contracts. However, if you are
integrating Spring with legacy code, then you may find that the dependencies defined in the code
require you to provide extra information to the Spring Framework.
Autowiring Your Bean
In all the examples so far, we have had to define explicitly, via the configuration file, how the individual
beans are wired together. If you don't like having to wire all your components together, then you can
have Spring attempt to do so automatically. By default, autowiring is disabled. To enable it, you specify
which method of autowiring you want to use by using the autowire attribute of the bean you want to
autowire.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home