<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Mdarim&#039;s Blog</title>
	<atom:link href="http://mdarim.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://mdarim.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Tue, 03 Nov 2009 15:15:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='mdarim.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Mdarim&#039;s Blog</title>
		<link>http://mdarim.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://mdarim.wordpress.com/osd.xml" title="Mdarim&#039;s Blog" />
	<atom:link rel='hub' href='http://mdarim.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Spring In action book summarization</title>
		<link>http://mdarim.wordpress.com/2009/11/03/spring-in-action-book-summarization/</link>
		<comments>http://mdarim.wordpress.com/2009/11/03/spring-in-action-book-summarization/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 15:09:48 +0000</pubDate>
		<dc:creator>mdarim</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[sprint]]></category>

		<guid isPermaLink="false">http://mdarim.wordpress.com/?p=4</guid>
		<description><![CDATA[Spring in action tutorial: &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- Chapter 1: Spring into action: &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- 1.1 What is Spring ?: - Spring is a lightweight dependency injection and aspect-oriented container and framework. - Main features of Spring: - Lightweight: - Spring is lightweight in terms of both size and overhead. - Objects in a Spring-enabled application often have no [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdarim.wordpress.com&amp;blog=10254229&amp;post=4&amp;subd=mdarim&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Spring in action tutorial:</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
Chapter 1: Spring into action:<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>1.1 What is Spring ?:<br />
- Spring is a lightweight dependency injection and aspect-oriented container<br />
and framework.<br />
- Main features of Spring:<br />
- Lightweight:<br />
- Spring is lightweight in terms of both size and overhead.<br />
- Objects in a Spring-enabled application often have no<br />
dependencies on Spring-specific classes.<br />
- Dependency Injection:<br />
- Spring promotes loose coupling through a technique known<br />
as dependency injection (DI). objects are passively given<br />
their dependencies instead of creating or looking for<br />
dependent objects for themselves.<br />
- Aspect-oriented:<br />
- Enables cohesive development by separating application<br />
business logic from system services, so the application<br />
objects will do what they are supposed to do -perform<br />
business logic- and nothing more.<br />
- Container:<br />
- Spring is a container in the sense that it contains and<br />
manages the lifecycle and configuration of application<br />
objects.<br />
- In Spring, you declare how each of your application objects<br />
should be create, how they should be configured, and how<br />
they should be configured, and how they should be associated<br />
with each other.<br />
- Framework:<br />
- Spring makes it possible to configure and compose complex applications<br />
from simpler components.</p>
<p>- Spring helps you develop loosely coupled application code,<br />
so (maintainability and testability).</p>
<p>1.1.1 Spring modules:<br />
- The Spring Framework is composed of several well-defined modules built<br />
on top of the core container.<br />
- Spring offers integration points with several other frameworks and libraries<br />
so you won&#8217;t have to write them yourself.</p>
<p>* The core container:<br />
- provides the fundamental functionality of the spring framework.<br />
- This module contains the BeanFactory which is the basis on which<br />
Spring&#8217;s DI is based.<br />
* Application context module:<br />
- This module makes Spring a framework by adding support for<br />
internationalization messages, application lifecycle events, and<br />
validation.<br />
* Spring&#8217;s AOP module:<br />
- Supports loose coupling of application objects.<br />
- Applicationwide concerns (such as transactions and security)<br />
are decoupled from the objects to which they are applied.<br />
* JDBC abstraction and the DAO module:<br />
- Abstracts a way the boilerplate code so that you can keep your database code<br />
clean and simple.<br />
- Uses Spring&#8217;s AOP module to provide transaction management services for<br />
objects in a Spring application.<br />
* Object-relational mapping (ORM) integration module:<br />
- Provides adapter for popular ORM frameworks, including Hibernate, JPA, JDO,<br />
and iBATIS SQL Maps.<br />
* Java Management Extensions (JMX):<br />
- Makes it easy to expose your application&#8217;s beans as JMX MBeans.<br />
* Java EE Connector API (JCA):<br />
- Abstracting away JCA&#8217;s boilerplate code into templates.<br />
* The Spring MVC framework:<br />
- Spring comes with its own very capable MVC framework that promotes Spring&#8217;s<br />
loosely coupled techniques in the web layer of an application.<br />
- integrates with several popular MVC frameworks.<br />
* Spring Portlet MVC:<br />
- Builds on Spring MVC to provide a set of controllers that<br />
support Java&#8217;s portlet API.<br />
* Spring&#8217;s web module:<br />
- provides special support classes for Spring MVC and Spring Portlet MVC.<br />
- Supports several web-oriented tasks, such as multipart file uploads and<br />
programmatic binding of request parameters to you business objects.<br />
- Integration support with Apache Struts and Java Server Faces (JSF).<br />
* Remoting:<br />
- Support enabling to expose the functionality of your java objects as<br />
remote objects, using many options, RMI, Hessian, Burlap, JAX-RPC, and<br />
Spring&#8217;s own HTTP Invoker.<br />
* Java Message Service (JMS):<br />
- Helps you send messages to JMS message queues and topics.</p>
<p>1.2 A Spring jump start:<br />
Listing 1.1: The interface for a greeting service:<br />
package com.springinaction.chapter01.hellp;<br />
public interface GreetingService {<br />
void sayGreeting();<br />
}<br />
Listing 1.2: GreetingServiceImpl, which prints a friendly greeting:<br />
package com.springinaction.chapter01.hello;<br />
public class GreetingServiceImpl implements GreetingService {<br />
private String greeting;<br />
public GreetingServiceImpl() {}<br />
public GreetingServiceImpl(String greeting) {<br />
this.greeting = greeting;<br />
}<br />
public void sayGreeting() {<br />
System.out.println(greeting);<br />
}<br />
public void setGreeting(String greeting){<br />
this.greeting = greeting;<br />
}<br />
}</p>
<p>- note that it is not apparent yet who will make the call to either the<br />
constructor or the setGreeting() method to set the property.<br />
- the Spring configuration file (hello.xml) tell the container how to configure<br />
the greeting service:<br />
Listing 1.3: configuring Hello world in Spring:<br />
&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;UTF-8&#8243; ?&gt;<br />
&lt;beans xmlns=&#8221;http://www.springframework.org/schema/beans&#8221;<br />
xmlns:xsi=&#8221;http://www.w3.org/2001/XMLSchema-instance&#8221;<br />
xsi:schemaLocation=&#8221;http://www.springframework.org/schema/beans<br />
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd&#8221;<br />
&gt;<br />
&lt;bean id=&#8221;greetingService&#8221;&gt;<br />
&lt;property name=&#8221;greeting&#8221; value=&#8221;Welcome to First application using Spring&#8221; /&gt;<br />
&lt;/bean&gt;<br />
&lt;/beans&gt;<br />
=====&gt;<br />
GreetingServiceImpl greetingService = new GreetingServiceImpl();<br />
greetingService.setGreeting(&#8220;Welcome to First application using Spring&#8221;);<br />
======<br />
or<br />
&lt;bean id=&#8221;greetingService&#8221;&gt;<br />
&lt;constructor-arg value=&#8221;Welcome to First application using Spring&#8221; /&gt;<br />
&lt;/bean&gt;<br />
======&gt;<br />
GreetingServiceImpl greetingService =<br />
new GreetingServiceImpl(&#8220;Welcome to First application using Spring&#8221;);<br />
=======</p>
<p>- The class that loads the Spring container and uses it to retrieve the greeting service:<br />
Listing 1.4: The Hello World main class:<br />
package com.springinaction.chapter01.hello;<br />
import org.springframework.beans.factory.BeanFactory;<br />
import org.springFramework.beans.factory.xml.XmlBeanFactory;<br />
import org.springframework.core.io.FileSystemResource;</p>
<p>public class HelloApp {<br />
public static void main(String[] args) throws Exception {<br />
BeanFactory factory =<br />
new XmlBeanFactory(new FileSystemResource(&#8220;hello.xml&#8221;));<br />
GreetingService greetingService =<br />
(GreetingService) factory.getBean(&#8220;greetingService&#8221;);<br />
greetingService.sayGreeting();<br />
}<br />
}</p>
<p>- The BeanFactory class used here is the Spring container.<br />
- This example is too simple because it only illustrates how to configure a bean<br />
by injecting a String value into a property.<br />
the real power of Spring lies in how beans can be injected into other beans using DI.</p>
<p>1.3: Understanding dependency injection:<br />
- DI is at the heart of the Spring Framework.<br />
- by applying DI in your projects, you&#8217;ll find that your code will become significantly<br />
simpler, easier to understand, and easier to test.</p>
<p>1.3.1: injecting dependencies:<br />
- Dependency injection involves giving an object its dependencies as opposed to an<br />
object having to acquire those dependencies on its own.<br />
- The key benefit of DI is loose coupling. if an object only knows about its dependencies<br />
by their interface (not their implementation or how they were instantiated) then the<br />
dependency can be swapped out with a different implementation without the depending object<br />
knowing the difference.<br />
- by doing this the injected object can be a local POJO, a remote web service, and EJB.</p>
<p>1.3.2: Dependency injection in action:<br />
- Classes need to know about each other somehow. So coupling is necessary, but<br />
it should be managed carefully.<br />
- A common technique used to reduce coupling is to hide implementation details behind<br />
interfaces so that the actual implementation class can be swapped out without impacting<br />
the client class.<br />
- The DI is all about: the responsibility of coordinating collaboration between dependent objects<br />
is transferred away from the objects themselves.<br />
- The act of creating associations between application components is referred to as wiring.<br />
In Spring there are many ways to wire components together, but the most common approach is<br />
via XML.<br />
- EX:<br />
&lt;bean id=&#8221;quest&#8221;/&gt;<br />
&lt;bean id=&#8221;knight&#8221;&gt;<br />
&lt;constructor-arg value=&#8221;Bedivere&#8221; /&gt;<br />
&lt;property name=&#8221;quest&#8221; ref=&#8221;quest&#8221;/&gt;<br />
&lt;/bean&gt;</p>
<p>- In a Spring application, a BeanFactory loads the bean definitions and wires the beans<br />
together. and because it is declared in an XML file, an XmlBeanFactory is the appropriate<br />
factory.</p>
<p>1.3.3: Dependency injection in enterprise applications:<br />
- You might choose to hide the lookup details behind a service locator.<br />
this would address the coupling concerns seen with the other approaches, but<br />
now the web layer is coupled to the service locator.<br />
- the key issue with all of these approaches is that the web layer component is too<br />
involved in obtaining its own dependencies. it knows too much about where the order<br />
service comes from and how it&#8217;s implemented.<br />
- if knowing too much about your dependencies leads to tightly coupled code, it<br />
stands to reason that knowing as little as possible about your dependencies leads<br />
to loosely coupled code.</p>
<p>1.4: Applying aspect-oriented programming:<br />
- Enables you to capture functionality that is used throughout your application in<br />
reusable components.<br />
- promotes separation of concerns within a software system.<br />
- in short, aspects ensure that POJOs remain plain.<br />
- it keeps the security, transaction, and loggin concerns from littering the<br />
application&#8217;s core business logic.</p>
<p>1.4.2: AOP in action:<br />
* Weaving the aspect:<br />
- first add the following namespace to already declared xml file:<br />
&lt;beans &#8230; http://www.springframework.org/schema/aop<br />
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd&#8221;&gt;</p>
<p>&lt;bean id=&#8221;minstrel&#8221;/&gt;<br />
&lt;aop:config&gt;<br />
&lt;aop:aspect ref=&#8221;minstrel&#8221;&gt;<br />
&lt;aop:pointcut id=&#8221;questPointcut&#8221;<br />
expression=&#8221;execution(* *.emarkOnQuest(..)) and target(bean)&#8221; /&gt;<br />
&lt;aop:before<br />
method=&#8221;singBefore&#8221;<br />
pointcut-ref=&#8221;questPointcut&#8221;<br />
arg-names=&#8221;bean&#8221; /&gt;<br />
&lt;aop:after-returning<br />
method=&#8221;singAfter&#8221;<br />
pointcut-ref=&#8221;questPointcut&#8221;<br />
arg-names=&#8221;bean&#8221; /&gt;<br />
&lt;/aop:aspect&gt;<br />
&lt;/aop:config&gt;<br />
&#8230;<br />
&lt;/beans&gt;</p>
<p>- The minstrel bean, which is a Minstrel, will provide the functionality of the aspect.<br />
- point-cut defines a pointcut that is triggered by the execution of an embarkOnQuest()<br />
method.<br />
- Then there are an advice. or how to apply the functionality.</p>
<p>1.5: Summary:<br />
- Spring aims to make enterprise Java development easier and to promote loosely coupled code.<br />
by using DI and AOP.<br />
- AOP enables you to centralize logic that would normally be scattered throughout an application<br />
in one place &#8211; an aspect. When Spring wires your beans together, these aspects can be woven<br />
in at runtime, effectively giving the beans new behavior.</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
Chapter 2: Basic bean wiring:<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>- objects are not responsible for finding or creating the other objects that they need to do<br />
their job. Instead, they are given references to the objects that they collaborate with by<br />
the container.<br />
- The act of creating these associations between application objects is the essence of<br />
dependency injection (DI) and is commonly referred to as wiring.</p>
<p>2.1: Containing your beans:<br />
- In a Spring application, objects are created, wired together, and live within the Spring container.<br />
- Spring comes with several container implementations that can be categorized into two distinct types,<br />
Bean factories (defined by the org.springframework.beans.factory.BeanFactory interface)<br />
are the simplest of containers, providing basic support for DI.<br />
Application contexts (defined by the org.springframework.context.ApplicationContext interface)<br />
providing application framework services, such as the ability to resolve textual messages from<br />
properties file and the ability to publish application events to interested event listeners.</p>
<p>2.1.1: Introducing the BeanFactory:<br />
- Most implementation of BeanFactory is org.springframework.beans.factory.xml.XmlBeanFactory;<br />
which loads its beans based on the definitions contained in an XML file.<br />
- To create an XmlBeanFactory, you must pass an instance of<br />
org.springframework.core.io.Resource to the constructor. There are several Resource implementations<br />
of this interface.<br />
- EX.<br />
BeanFactory factory = new XmlBeanFactory(new FileSystemResource(&#8220;c:/beans.xml&#8221;));<br />
this tells the bean factory to read the bean definitions from the XML file.<br />
but the bean factory doesn&#8217;t instantiate the beans just yet.<br />
the beans themselves will not be instantiated until they are needed.<br />
- To retrieve a bean from a BeanFactory, simply call the getBean() method, passing the ID<br />
of the bean you want to retrieve:<br />
MyBean myBean = (MyBean) factory.getBean(&#8220;myBean&#8221;);<br />
when getBean() is called, the factory will instantiate the bean and set the bean&#8217;s properties<br />
using DI. Thus begins the life of a bean within the Spring container.</p>
<p>2.1.2: Working with an application context:<br />
- To gain the full power of the Spring Framework.<br />
- An ApplicationContext is much the same as a BeanFactory. Both load bean definitions, wire beans<br />
together, and dispense beans upon request. But an ApplicationContext offers much more:<br />
- provides a means for resolving text messages.<br />
- provides a generic way to load file resources, such as images.<br />
- can publish events to beans that are registered as listeners.<br />
- An ApplicationContext is preferred over a BeanFactory in nearly all applications.<br />
- Among the many implementations of ApplicationContext are three that are commonly<br />
used:<br />
- ClassPathXmlApplicationContext:<br />
- loads a context definition from an XML file located in the classpath.<br />
- treating context definition files as classpath resources.<br />
- FileSystemXmlApplicationContext:<br />
- loads a context definition from an XML file in the file system.<br />
- XmlWebApplicationContext:<br />
- loads context definitions from an XML file contained within a web application.<br />
- EX.<br />
ApplicationContext context =<br />
new FileSystemXmlApplicationContext(&#8220;c:/foo.xml&#8221;); // will look for foo.xml in a specific<br />
// location within the file system.</p>
<p>or from within the application&#8217;s classpath using:<br />
ApplicationContext context =<br />
new ClassPathXmlApplicationContext(&#8220;foo.xml&#8221;); // will look for foo.xml anywhere in the<br />
// classpath (including JAR files).</p>
<p>- and then you can use the getBean() method to retrieve a bean.<br />
- The ApplicationContext interface extends the BeanFactory interface.<br />
- The big difference between an application context and a bean factory is how singleton beans<br />
are loaded. A bean factory lazily loads all beans, deferring bean creation until the getBean()<br />
method is called. An application context is a bit smarter and preloads all singleton beans upon<br />
context startup. By preloading singleton beans, you ensure that they will be ready to use when<br />
needed &#8211; your application won&#8217;t have to wait for them to be created.</p>
<p>2.1.3: A bean&#8217;s life:<br />
- A bean goes through several steps between creating and destruction in the Spring container,<br />
each step is an opportunity to customize how the bean is managed in Spring, The steps taken<br />
in the life of a bean are:<br />
1- Instantiate                : Spring instantiates the bean.<br />
2- Populate properties            : Spring injects the bean&#8217;s properties.<br />
3- Set Bean name            : if the bean implements BeanNameAware, Spring<br />
passes the bean&#8217;s ID to setBeanName().<br />
4- Set bean factory            : if the bean implements BeanFactoryAware, Spring<br />
passes the bean factory to setBeanFactory().<br />
5- Postprocess (Before initialization)    : if there are any BeanPostProcessors, Spring<br />
calls their postProcessBeforeInitialization()<br />
method.<br />
6- Initialize beans            : if the bean implements InitializingBean, its<br />
afterPropertiesSet() method will be called.<br />
if the bean has a custom init method declared, the<br />
specified initialization method will be called.<br />
7- PostProcess (After initialization)    : if there are any BeanPostprocessors, Spring calls<br />
their postProcessAfterInitialization() method.<br />
8- Bean is ready to use            : At this point the bean is ready to be used by<br />
the application and will remain in the bean factory<br />
untill it is no longer needed.<br />
9- Destroy bean                         : if the bean implements DisposableBean, its<br />
destroy() method will be called.<br />
if the bean has a custom destory-method<br />
declared, the specified method will be called.</p>
<p>- The lifecycle of a bean in a Spring application context extends the lifecycle of<br />
a factory-contained bean by adding a step to make the bean application context aware.<br />
so after step 4 above there are a step:<br />
- ApplicationContextAware&#8217;s setApplicationContext(): if the bean implements<br />
the ApplicationContextAware interface, the setApplicationContext() method is called.</p>
<p>2.2: Creating beans:</p>
<p>2.2.1: Declaring a simple bean:<br />
- Declared a bean in the Spring configuration file:<br />
&lt;bean id=&#8221;duke&#8221; /&gt;<br />
it equals to:<br />
new com.springinaction.springidol.Juggler();</p>
<p>2.2.2: Injecting through constructors:<br />
- Ceclared a bean with constructor:<br />
&lt;bean id=&#8221;duke&#8221;&gt;<br />
&lt;constructor-arg value=&#8221;15&#8243; /&gt;<br />
&lt;/bean&gt;</p>
<p>- Injecting object references with constructors:<br />
&lt;bean id=&#8221;sonnet29&#8243; /&gt;<br />
&lt;bean id=&#8221;duke&#8221;&gt;<br />
&lt;constructor-arg value=&#8221;15&#8243; /&gt;<br />
&lt;constructor-arg ref=&#8221;sonnet29&#8243; /&gt;<br />
&lt;/bean&gt;</p>
<p>same as:<br />
Poem sonnet29 = new Sonnet29();<br />
Performer duke = new PoeticJuggler(15, sonnet29);</p>
<p>* Constructor or setter injection: how do you choose ?<br />
- The constructor-injection side:<br />
- Constructor injection enforces a strong dependency contract. a bean cannot be<br />
instantiated without being given all of its dependencies.<br />
- Because all of the bean&#8217;s depenencies are set through its constructor, there&#8217;s no<br />
need for superfluous setter methods. This helps keep the lines of code at a minimum.<br />
- By only allowing properties to be set through the constructor, you are, effectively,<br />
making those properties immutable, preventing accidental change in the course of<br />
application flow.<br />
- The setter injection side:<br />
- if a bean has several dependencies, the constructor&#8217;s parameter list can be<br />
quite lengthy.<br />
- if there are several ways to construct a valid object, it can be hard to come up<br />
with unique constructors, since constructor signatures vary only be the number and type<br />
of parameters.<br />
- if a constructor takes two or more parameters of the same type, it may be difficult to<br />
determine what each parameter&#8217;s purpose is.<br />
- constructor injection does not lend itself readily to inheritance. A bean&#8217;s constructor<br />
will have to pass parameters to super() in order to set private properties in the parent<br />
object.</p>
<p>- note that constructor injection is a surefire way to guarantee that a bean is fully configured<br />
before it is used.</p>
<p>2.3: injecting into bean properties:<br />
- Spring can take advantage of a property&#8217;s setter method to configure the property&#8217;s value<br />
through setter injection.</p>
<p>2.3.1: injecting simple values:<br />
- Bean properties can be configured in Spring using the &lt;property&gt; element.<br />
- Ex.<br />
&lt;bean id=&#8221;kenny&#8221;&gt;<br />
&lt;property name=&#8221;song&#8221; value=&#8221;Jingle Bells&#8221; /&gt;<br />
&lt;/bean&gt;</p>
<p>- Spring will determine the correct type for the value based on the property&#8217;s type.</p>
<p>2.3.2: Referencing other beans:<br />
&lt;bean id=&#8221;saxophone&#8221;/&gt;<br />
&lt;bean id=&#8221;kenny&#8221;&gt;<br />
&lt;property name=&#8221;instrument&#8221; ref=&#8221;saxophone&#8221;/&gt;<br />
&lt;/bean&gt;</p>
<p>* Injecting inner beans:<br />
- inner beans are beans that are defined within the scope of another bean.<br />
- Ex.<br />
&lt;bean id=&#8221;kenny&#8221;&gt;<br />
&lt;property name=&#8221;song&#8221; value=&#8221;Jingle Bells&#8221;/&gt;<br />
&lt;property name=&#8221;instrument&#8221;&gt;<br />
&lt;bean/&gt;<br />
&lt;/property&gt;<br />
&lt;/bean&gt;<br />
- An inner bean is defined by declaring a &lt;bean&gt; element directly as a child of the<br />
&lt;property&gt; element to which it will be injected.<br />
- You can also wire inner beans into constructor arguments.<br />
&lt;bean id=&#8221;duke&#8221;&gt;<br />
&lt;constructor-arg&gt;<br />
&lt;bean/&gt;<br />
&lt;constructor-arg&gt;<br />
&lt;/bean&gt;</p>
<p>- The above highlights the main drawback of using inner beans: they can&#8217;t be reused.<br />
Inner beans are only useful for injection once and can&#8217;t be referred to by other beans.<br />
- Also using inner-bean definitions has a negative impact on the readability of the XML<br />
in the Spring context files.</p>
<p>2.3.3: Wiring collections:<br />
- Springs offers four types of collection configuration elements that come in handy when<br />
configuring collections of values.<br />
- Spring allows for injecting several kinds of collections:<br />
- &lt;list&gt;        : Wiring a list of values, allowing duplicates.<br />
- &lt;set&gt;         : Wiring a set of values, ensuring no duplicates.<br />
- &lt;map&gt;         : Wiring a collection of name-value pairs where name and value can<br />
be of any type.<br />
- &lt;props&gt;       : Wiring a collection of name-value pairs where the name and value are<br />
both Strings.</p>
<p>- The &lt;list&gt; and &lt;set&gt; elements are useful when configuring properties that are either arrays<br />
or some implementation of java.util.Collection.<br />
- For &lt;map&gt; and &lt;props&gt;, these two elements correspond to collections that are java.util.Map<br />
and java.util.Properties.</p>
<p>* Lists and arrays:<br />
&lt;property name=&#8221;instruments&#8221;&gt;<br />
&lt;list&gt;<br />
&lt;ref bean=&#8221;guitar&#8221;/&gt;<br />
&lt;ref bean=&#8221;cymbal&#8221;/&gt;<br />
&lt;ref bean=&#8221;harmonica&#8221;/&gt;<br />
&lt;/list&gt;<br />
&lt;/property&gt;<br />
- The &lt;list&gt; element contains one or more values. here &lt;ref&gt; elements are used to define<br />
the values as references to other beans in the Spring context.<br />
- It&#8217;s also possible to use other value-setting Spring elements as teh members of a &lt;list&gt;,<br />
including &lt;value&gt;, &lt;bean&gt;, and &lt;null/&gt;, or another &lt;list&gt;.<br />
- Instrument[] or java.util.List&lt;Instrument&gt;</p>
<p>* Sets:<br />
- &lt;set&gt; ensures that each of its members is unique.<br />
&lt;set&gt; &#8230; &lt;/set&gt;.<br />
- The &lt;set&gt; element can be used to configure properties whose type is an array or an implementation<br />
of java.util.Collection.</p>
<p>* Maps:<br />
&lt;map&gt;<br />
&lt;entry key=&#8221;GUITAR&#8221; value-ref=&#8221;guitar&#8221;/&gt;<br />
&lt;entry key=&#8221;CYMBAL&#8221; value-ref=&#8221;cymbal&#8221;/&gt;<br />
&lt;entry key=&#8221;HARMONICA&#8221; value-ref=&#8221;harmonica&#8221;/&gt;<br />
&lt;/map&gt;<br />
- An &lt;entry&gt; in a &lt;map&gt; is made up of a key and a value, either of which can be a primitive<br />
value or a reference to another bean. These attributes help specify the keys and values of<br />
an &lt;entry&gt;:<br />
- key           : Specifies the key of the map entry as a String.<br />
- key-ref       : Specifies the key of the mpa entry as a reference to a bean in the<br />
Spring context.<br />
- value         : Specifies the value of the map entry as a String.<br />
- value-ref     : Specifies the value of the map entry as a reference to a bean in the<br />
Spring context.<br />
- &lt;map&gt; is only one way to inject key-value pairs into bean properties when either of the<br />
objects is not a String.</p>
<p>* Properties:<br />
&lt;props&gt;<br />
&lt;prop key=&#8221;GUITAR&#8221;&gt;STRUM STRUM STRUM&lt;/prop&gt;<br />
&lt;prop key=&#8221;CYMBAL&#8221;&gt;CRASH CRASH CRASH&lt;/prop&gt;<br />
&lt;prop key=&#8221;HARMONICA&#8221;&gt;HUM HUM HUM&lt;/prop&gt;<br />
&lt;/props&gt;</p>
<p>The &lt;props&gt; element constructs a java.util.Properties value where each member is defined by<br />
a &lt;prop&gt; element.</p>
<p>2.3.4: Wiring nothing (null):<br />
- To explicitly wire null into the property. or to override an autowired property value<br />
you can use:<br />
&lt;property name=&#8221;someNonNullProperty&#8221;&gt;&lt;null/&gt;&lt;/property&gt;</p>
<p>2.4: Autowiring:<br />
- You can have Spring automatically figure out how to wire beans together by setting the<br />
autowire property on each &lt;bean&gt; that you want Spring to autowire.</p>
<p>2.4.1: The four types of autowiring:<br />
- Spring provides four flavors of autowiring:<br />
- byName        : Attempts to find a bean in the container whose name (or ID) is<br />
the same as teh name of the property being wired. if a matching<br />
bean is not found, the property will remain unwired.<br />
- byType        : Attempts to find a single bean in the container whose type matches<br />
the type of the property being wired. if no matching bean is found,<br />
the property will not be wired. if more than one bean matches, an<br />
org.springframework.beans.factory.UnsatisfiedDependencyException<br />
will be thrown.<br />
- constructor   : Tries to match up one or more beans in the container with the parameters<br />
of one of the constructors of the bean being wired. In the event of<br />
ambiguous beans or ambiguous constructors, an org.springframework.<br />
bean.factory.UnsatisfiedDependencyException will be thrown.<br />
- autodetect    : Attempts to autowire by constructor first and then using byType.<br />
Ambiguity is handled the same way as with constructor and byType wiring.</p>
<p>* Autowiring by name:<br />
- EX.<br />
&lt;bean id=&#8221;kenny&#8221; autowire=&#8221;byName&#8221; /&gt;<br />
In setting the autowire property to byName, you are telling Spring to consider all properties<br />
of kenny and look for beans declared with the same names as the properties.<br />
- The limitation of using byName autowiring is that it assumes that you&#8217;ll have a bean whose<br />
name is the same as the property of another bean that you&#8217;ll be injecting into.</p>
<p>* Autowiring by type:<br />
- Autowiring using byType works in a similar way to byName, except that instead of considering<br />
a property&#8217;s name, the property&#8217;s type is examined. When attempting to autowire a property by<br />
type, Spring will look for beans whose type is assignable to the property&#8217;s type.<br />
- But there is a limitation to autowiring by type. What happens if Spring finds more than one<br />
bean whose type is assignable to the autowired property?, in such a case, Spring will throw<br />
an exception. so you are allowed to have only one bean configured that matches the autowired<br />
property.</p>
<p>* Using constructor autowiring:<br />
- If your bean is configured using constructor injection, you may choose to put away the<br />
&lt;constructor-arg&gt; elements and let Spring automatically choose constructor arguments from<br />
beans in the Spring context.<br />
- Ex.<br />
&lt;bean id=&#8221;duke&#8221; autowire=&#8221;constructor&#8221;/&gt;<br />
The &lt;constructor-arg&gt; elements are gone and the autowire attribute has been set to constructor.<br />
This tells Spring to look at PoeticJuggler&#8217;s constructors and try to find beans in the Spring<br />
configuration to satisfy the arguments of one of the constructors.<br />
- Autowiring by constructor shares the same limitations as byType. That is, Spring will not<br />
attempt to guess which bean to autowire when it finds multiple beans that match a<br />
constructor&#8217;s arguments. Furthermore, if a constructor has multiple constructors, any of which<br />
can be satisfied by autowiring, Spring will not attempt to guss which constructor to use.</p>
<p>* Autodetect autowiring:<br />
&lt;bean id=&#8221;duke&#8221; autowire=&#8221;autodetect&#8221;/&gt;<br />
When a bean has bean configured to autowire by autodetect, Spring will attempt to autowire<br />
by constructor first. if a suitable constructor-bean match can&#8217;t be found then Spring will<br />
attempt to autowire by type.</p>
<p>* Autowiring by default:<br />
- By default, beans will not be autowired unless you set the autowire attribute.<br />
However, you can set a default autowiring for all beans within the Spring context by setting<br />
default-autowire on the root &lt;beans&gt; element:<br />
&lt;beans default-autowire=&#8221;byName&#8221;&gt;<br />
&#8230;<br />
&lt;/beans&gt;<br />
Set this way, all beans will be autowired using byName unless specified otherwise.</p>
<p>2.4.2: Mixing auto with explicit wiring:<br />
- You explicitly wire some properties with autowiring.<br />
- Mixing automatic and explicit wiring is also a great way to deal with ambiguous autowiring that<br />
may occur when autowiring using byType.<br />
- Note that when using constructor autowiring, you must let Spring wire all of the constructor<br />
arguments &#8211; you cannot mix &lt;constructor-arg&gt; elements with constructor autowiring.</p>
<p>2.4.3: To autowire or not to autowire:<br />
- Although it cut down on the amount of manual configuration required when writing the bean<br />
wiring file, it may lead to some problems. As you&#8217;ve already seen, autowiring by type or<br />
constructor imposes a restriction where you may only have one bean of a given type.<br />
And autowiring by name forces you to name your beans to match the properties that they&#8217;ll<br />
be autowired into.<br />
- The most serious shortcoming of autowiring is that it lacks clarity.</p>
<p>2.5: Controlling bean creating:<br />
- Spring provides a handful of options for creating beans that go beyond what we&#8217;ve seen<br />
so far. You can do the following:<br />
- Control how many instances of a specific bean are created, whether it is one<br />
instance for the entire application, one instance per user request, or<br />
a brand-new instance each time teh bean is used.<br />
- Create beans from static factory methods instead of public constructors.<br />
- Initialize a bean after it is created and clean up just before it is<br />
destroyed.</p>
<p>2.5.1: Bean scoping:<br />
- By default, all Spring beans are singletons. That is, when the container dispenses a<br />
bean (either through wiring or as the result of a call to the container&#8217;s getBean() method)<br />
it will always hand out the exact same instance of the bean.<br />
- To force Spring to produce a new bean instance each time one is needed, you should declare<br />
the bean&#8217;s scope attribute to be prototype.<br />
- Ex.<br />
&lt;bean id=&#8221;saxophone&#8221; scope=&#8221;prototype&#8221;/&gt;</p>
<p>- Spring&#8217;s bean scopes let you declare the scope under which beans are created without<br />
hardcoding the scoping rules in the bean class itself:<br />
- singleton     : Scopes the bean definition to a single instance per Spring container<br />
(default).<br />
- prototype     : Allows a bean to be instantiated any number of times (once per use).<br />
- request       : Scopes a bean definition to an HTTP request. Only valid when used<br />
with a web-capable Spring context (such as with Spring MVC).<br />
- session       : Scopes a bean definition to an HTTP request.<br />
- global-session: Scopes a bean definition to a global HTTP session. Only valid when<br />
used in a portlet context.</p>
<p>- For the most part, you&#8217;ll probably want to leave scoping to the default singleton,<br />
but prototype scope may be useful in situations where you want to use Spring as a factory<br />
for new instances of domain objects.</p>
<p>2.5.2: Creating beans from factory methods:<br />
- Ex.<br />
package com.spring;</p>
<p>public class Stage {<br />
private Stage() {}<br />
private static class StageSingletonHolder {<br />
static Stage instance = new Stage();<br />
}<br />
public static Stage getInstance() {<br />
return StageSingletonHolder.instance;<br />
}<br />
}</p>
<p>- Fortunately, the &lt;bean&gt; element has a factory-method attribute that lets you specify a static<br />
method to be invoked instead of the constructor to create an instance of a class.<br />
- Ex.<br />
&lt;bean id=&#8221;theStage&#8221; factory-method=&#8221;getInstance&#8221; /&gt;</p>
<p>2.5.3: Initializing and destroying beans:<br />
- To define setup and teardown for a bean, simply declare the &lt;bean&gt; with init-method and/or<br />
destory-method parameters.<br />
- The init-method attribute specifies a method that is to be called on the bean immediately<br />
upon instantiation.<br />
- destroy-method specifies a method that is called just before a bean is removed from the<br />
container.<br />
- Ex.<br />
&lt;bean id=&#8221;kenny&#8221;<br />
init-method=&#8221;tuneInstrument&#8221;<br />
destroy-method=&#8221;cleanInstrument&gt;<br />
&#8230;<br />
&lt;/bean&gt;</p>
<p>* Defaulting init-method and destroy-method:<br />
- If many of the beans in a context definition file will have initialization or destroy<br />
methods with the same name, you can take advantage of the default-init-method and<br />
default-destroy-method attributes on the &lt;bean&gt; element:<br />
&lt;beans &#8230;<br />
default-init-method=&#8221;&#8230;&#8221;<br />
default-destroy-method=&#8221;&#8230;&#8221;&gt;<br />
&#8230;<br />
&lt;/beans&gt;<br />
- In this case, we&#8217;re asking Spring to initialize all beans in the context definiton file by<br />
calling the &#8230; methods and also to tear them down with &#8230;, if those methods exist &#8211; otherwise<br />
nothing happends.</p>
<p>* InitializingBean and DisposableBean:<br />
- As an option to init-method and destroy-method, we could also rewrite the class to<br />
implement two special Spring interfaces: InitializingBean and DisposableBean.<br />
- The Spring container treats beans that implement these interfaces in a special way by<br />
allowing them to hook into the bean lifecycle.<br />
- The InitializingBean interface mandates that the class implements afterPropertiesSet().<br />
This method will be called once all specified properties for the bean have bean set.<br />
This makes it possible for the bean to perform initialization that can&#8217;t be performed<br />
until the bean&#8217;s properties have been completely set.<br />
- Similarly, DisposableBean requires that a destroy() method be implemented.<br />
The destroy() method will be called on the other end of the bean&#8217;s lifecycle,<br />
when the bean is disposed of by the container.<br />
- The benefit of using these lifecycle interfaces is that Spring container is able to<br />
automatically detect beans that implement them without any external configuration.<br />
However, the disadvantage of implementing these interfaces is that you couple your<br />
application&#8217;s beans to Spring&#8217;s API.</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
Chapter 3: Advanced bean wiring:<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>3.1: Declaring parent and child beans:<br />
- The &lt;bean&gt; element provides two special attributes:<br />
- parent        : Indicates the id of a &lt;bean&gt; that will be the parent of the<br />
&lt;bean&gt; with the parent attribute. The parent attribute is to &lt;bean&gt;<br />
what extends is to a Java class.<br />
- abstract      : If set to true, indicates that the &lt;bean&gt; declaration is abstract.<br />
That is, it should never be instantiated by Spring.</p>
<p>3.1.1: Abstracting a base bean type:<br />
- If you have two &lt;bean&gt;s declared in Spring with virtually the same XML. or the only<br />
difference between these two &lt;bean&gt;s are their ids.<br />
- When you have two beans with the same type and have properties that are injected with<br />
the same values.<br />
- Ex.<br />
&lt;bean id=&#8221;base&#8221; abstract=&#8221;true&#8221;&gt;<br />
&lt;property name=&#8221;instrument&#8221; ref=&#8221;saxophone&#8221; /&gt;<br />
&lt;property name=&#8221;song&#8221; value=&#8221;Jingle Bells&#8221; /&gt;<br />
&lt;/bean&gt;</p>
<p>- The true attribute for abstract tells Spring to not try to instantiate this bean.<br />
even if it is explicitly requested from the container. In many ways this is the same<br />
as an abstract class in Java, which can&#8217;t be instantiated.<br />
- We are now able to declare the kenny and david beans as follows:<br />
&lt;bean id=&#8221;kenny&#8221; parent=&#8221;base&#8221; /&gt;<br />
&lt;bean id=&#8221;david&#8221; parent=&#8221;base&#8221; /&gt;<br />
- The parent attribute indicates that both the kenny and david beans will inherit their<br />
definition from the base bean.<br />
- The parent bean doesn&#8217;t have to be abstract. It&#8217;s entirely possible to create sub-beans<br />
that extend a concrete bean.</p>
<p>* Overriding inherited properties:<br />
- Ex.<br />
&lt;bean id=&#8221;frank&#8221; parent=&#8221;base&#8221;&gt;<br />
&lt;property name=&#8221;song&#8221; value=&#8221;Mary had a little lamb&#8221; /&gt;<br />
&lt;/bean&gt;</p>
<p>3.1.2:</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mdarim.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mdarim.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mdarim.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mdarim.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mdarim.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mdarim.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mdarim.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mdarim.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mdarim.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mdarim.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mdarim.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mdarim.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mdarim.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mdarim.wordpress.com/4/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdarim.wordpress.com&amp;blog=10254229&amp;post=4&amp;subd=mdarim&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mdarim.wordpress.com/2009/11/03/spring-in-action-book-summarization/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/735173cd7292a58743c179808df1e711?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mdarim</media:title>
		</media:content>
	</item>
		<item>
		<title>Mohamed Sulibi Home Page</title>
		<link>http://mdarim.wordpress.com/2009/11/03/hello-world/</link>
		<comments>http://mdarim.wordpress.com/2009/11/03/hello-world/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 15:01:15 +0000</pubDate>
		<dc:creator>mdarim</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Welcome All<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdarim.wordpress.com&amp;blog=10254229&amp;post=1&amp;subd=mdarim&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Welcome All</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mdarim.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mdarim.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mdarim.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mdarim.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mdarim.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mdarim.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mdarim.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mdarim.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mdarim.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mdarim.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mdarim.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mdarim.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mdarim.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mdarim.wordpress.com/1/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdarim.wordpress.com&amp;blog=10254229&amp;post=1&amp;subd=mdarim&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mdarim.wordpress.com/2009/11/03/hello-world/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/735173cd7292a58743c179808df1e711?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mdarim</media:title>
		</media:content>
	</item>
	</channel>
</rss>
