Containing your beans eAppcaen8M Pre-inialiai ePoenaaio readytouse athaoeri ● Figure 1.5 Abear goes thro ral steps between creation and destruction in the Spring naged in Spring. With an application context in hand,you can retrieve beans from the Spring con- tainer by calling the context's getBean()method. Now that you know the basics of how to create a Spring container,let's take a closer look at the lifecycle of a bean in the bean container. 1.2.2 A bean's life In a traditional Java application,the lifecycle of a bean is simple.Java's new keyword is used to instantiate the bean (or perhaps it's deserialized)and it's ready to use.Once the bean is no longer in use,it's eligible for garbage collection and eventually goes to the big bit bucket in the sky. In contrast,the lifecycle of a bean within a Spring container is more elaborate.It's important to understand the lifecycle of a Spring bean,because you may want to take advantage of some of the opportunities that Spring offers to customize how a bean is created.Figure 1.5 shows the startup lifecycle of a typical bean as it's loaded into a Spring application context. As you can see,a bean factory performs several setup steps before a bean is ready to use.Breaking down figure 1.5 in more detail: 1 Spring instantiates the bean. 2 Spring injects values and bean references into the bean's properties. 3 If the bean implements BeanNameAware,Spring passes the bean's ID to the set- BeanName()method. 4 If the bean implements BeanFactoryAware,Spring calls the setBeanFactory() method,passing in the bean factory itself
Containing your beans 19 With an application context in hand, you can retrieve beans from the Spring container by calling the context’s getBean() method. Now that you know the basics of how to create a Spring container, let’s take a closer look at the lifecycle of a bean in the bean container. 1.2.2 A bean’s life In a traditional Java application, the lifecycle of a bean is simple. Java’s new keyword is used to instantiate the bean (or perhaps it’s deserialized) and it’s ready to use. Once the bean is no longer in use, it’s eligible for garbage collection and eventually goes to the big bit bucket in the sky. In contrast, the lifecycle of a bean within a Spring container is more elaborate. It’s important to understand the lifecycle of a Spring bean, because you may want to take advantage of some of the opportunities that Spring offers to customize how a bean is created. Figure 1.5 shows the startup lifecycle of a typical bean as it’s loaded into a Spring application context. As you can see, a bean factory performs several setup steps before a bean is ready to use. Breaking down figure 1.5 in more detail: 1 Spring instantiates the bean. 2 Spring injects values and bean references into the bean’s properties. 3 If the bean implements BeanNameAware, Spring passes the bean’s ID to the setBeanName() method. 4 If the bean implements BeanFactoryAware, Spring calls the setBeanFactory() method, passing in the bean factory itself. DisposableBean’s destroy() Call custom destroy-method Bean is ready to use Container is shut down Figure 1.5 A bean goes through several steps between creation and destruction in the Spring container. Each step is an opportunity to customize how the bean is managed in Spring. Licensed to Christian Cederquist <chrisman@kaus.dk>
CHAPTER1 Springing into action 5 If the bean implements ApplicationcontextAware,Spring will call the set- Applicationcontext()method,passing in a reference to the enclosing appli- cation context 6 If any of the beans implement the BeanpostProcessor interface,Spring calls their postProcessBeforeInitialization()method. 7 If any beans implement the InitializingBean interface,Spring calls their afterPropertiesset()method.Similarly,if the bean was declared with an init-method,then the specified initialization method will be called. 8 If there are any beans that implement Beanpostprocessor,Spring will call their postProcessAfterInitialization()method. At this point,the bean is ready to be used by the application and will remain in the application context until the application context is destroyed. 10 If any beans implement the DisposableBean interface,then Spring will call their destroy()methods.Likewise,if any bean was declared with a destroy- method,then the specified method will be called. Now you know how to create e and load a Spring container.But an empty container isn't much good by itself;it doesn't contain anything unless you put something in it. To achieve the benefits of Spring DI,we must wire our application objects into the Spring container.We'll go into bean wiring in more detail in chapter 2. But first,let's survey the modern Spring landscape to see what the Spring Frame- work is made up of and to see what the latest versions of Spring have to offer. 1.3 Surveying the Spring landscape As you've seen,the Spring Framework is focused on simplifying enterprise Java devel- opment through dependency injection,aspect-oriented programming,and boiler- plate reduction.Even if that were all that Spring did,it'd be worth using.But there's more to Spring than meets the eye. Within the Spring Framework proper,you'll find several ways that Spring can ease Java development.But beyond the Spring Framework itself is a greater ecosystem of projects that build upon the core framework,extending Spring into areas such as web services,OSGi,Flash,and even.NET. Let's first break down the core Spring Framework to see what it brings to the table.Then we'll expand our sights to review the other members of the greater Spring portfolio. 13.1 Spring modules The Spring Framework is composed of several distinct modules.When you download and unzip the Spring Framework distribution,you'll find 20 different JAR files in the dist directory,as shown in figure 1.6. The 20 JAR files that make up Spring can be arranged in one of six different cate- gories of functionality,as illustrated in figure 1.7
20 CHAPTER 1 Springing into action 5 If the bean implements ApplicationContextAware, Spring will call the setApplicationContext() method, passing in a reference to the enclosing application context. 6 If any of the beans implement the BeanPostProcessor interface, Spring calls their postProcessBeforeInitialization() method. 7 If any beans implement the InitializingBean interface, Spring calls their afterPropertiesSet() method. Similarly, if the bean was declared with an init-method, then the specified initialization method will be called. 8 If there are any beans that implement BeanPostProcessor, Spring will call their postProcessAfterInitialization() method. 9 At this point, the bean is ready to be used by the application and will remain in the application context until the application context is destroyed. 10 If any beans implement the DisposableBean interface, then Spring will call their destroy() methods. Likewise, if any bean was declared with a destroymethod, then the specified method will be called. Now you know how to create and load a Spring container. But an empty container isn’t much good by itself; it doesn’t contain anything unless you put something in it. To achieve the benefits of Spring DI, we must wire our application objects into the Spring container. We’ll go into bean wiring in more detail in chapter 2. But first, let’s survey the modern Spring landscape to see what the Spring Framework is made up of and to see what the latest versions of Spring have to offer. 1.3 Surveying the Spring landscape As you’ve seen, the Spring Framework is focused on simplifying enterprise Java development through dependency injection, aspect-oriented programming, and boilerplate reduction. Even if that were all that Spring did, it’d be worth using. But there’s more to Spring than meets the eye. Within the Spring Framework proper, you’ll find several ways that Spring can ease Java development. But beyond the Spring Framework itself is a greater ecosystem of projects that build upon the core framework, extending Spring into areas such as web services, OSGi, Flash, and even .NET. Let’s first break down the core Spring Framework to see what it brings to the table. Then we’ll expand our sights to review the other members of the greater Spring portfolio. 1.3.1 Spring modules The Spring Framework is composed of several distinct modules. When you download and unzip the Spring Framework distribution, you’ll find 20 different JAR files in the dist directory, as shown in figure 1.6. The 20 JAR files that make up Spring can be arranged in one of six different categories of functionality, as illustrated in figure 1.7. Licensed to Christian Cederquist <chrisman@kaus.dk>
Surveying the Spring landscape 00a □dist Name 93P newo -3.0.5.RELEASE.jar work.beans-3.0.5.RELEASEJar ora sprinaframework context-3 0.5 RELEASE iar org.springframework.context.support-3.0.S.RELEASE.jar org.springframework.core-3.0.5.RELEASE.ja org-springframework.expression-3.0.5.RELEASE.jar org-springframewo ora soringframework.oxm-3.0.5.RELEASE.jar org.springframework.test-3.0.5.RELEASE.jar org.springframework.transaction-3.0.5.RELEASEjar org.springframework.web-3.0.5.RELEASE.jar -3.0.5.RELEASE.ja Figure 1.6 The JAR files that come with the Spring Framework distribution When taken as a whole,these modules give you everything you need to develop enterprise-ready applications.But you don't have to base your application fully on the Spring Framework.You're free to choose the modules that suit your application and Data access integration Web and remoting JDBC ORM Web Servlet OXM JMS Struts Instrumentatio AOP pect nstrument Core Spring containe Beans Core Context Expression Testing Test Figure 1.7 The Spring Framework is made up of six well-defined modules
Surveying the Spring landscape 21 When taken as a whole, these modules give you everything you need to develop enterprise-ready applications. But you don’t have to base your application fully on the Spring Framework. You’re free to choose the modules that suit your application and Figure 1.6 The JAR files that come with the Spring Framework distribution Data access & integration Web and remoting Core Spring container support Figure 1.7 The Spring Framework is made up of six well-defined modules. Licensed to Christian Cederquist <chrisman@kaus.dk>
CHAPTER1 Springing into action look to other options when Spring doesn't fit the bill.Spring even offers integration points with several other frameworks and libraries so that you won't have to write them vourself. Let's take a look at each of Spring's modules,one at a time,to see how each fits in the overall Spring picture. CORE SPRING CONTAINER The centerpiece of the Spring Framework is a container that manages how the beans in a Spring-enabled application are created,configured,and managed.Within this module you'll find the Spring bean factory,which is the portion of Spring that pro- vides dependency injection.Building upon the bean factory,you'll find several imple mentations of Spring's application context,each of which provides a different way to configure spring. In addition to the bean factory and application context,this module also supplies many enterprise services such as email,JNDI access,EJB integration,and scheduling. As you can see,all of Spring's modules are built on top of the core container.You'll implicitly use these classes when you configure your application.We'll discuss the core module throughout this book, starting in chapter 2 where we dig deep into Spring dependency injection. SPRING'S AOP MODULE Spring provides rich support for aspect-oriented programming in its AOP module This module serves as the basis for developing your own aspects for your Spring enabled application.Like DI,AOP supports loose coupling of application objects.But with AOP,application-wide concerns(such as transactions and security)are decoupled from the objects to which they're applied. We'll dig into Spring's AOP support in chapter 4. DATA ACCESS AND INTEGRATION Working with JDBC often results in a lot of boilerplate code that gets a connection creates a statement,processes a result set,and then closes the connection.Spring's JDBC and data access objects(DAO)module abstracts away the boilerplate code so that you can keep your database code clean and simple,and prevents problems that resul from a failure to close database resources.This module also builds a layer of meaning- ful exceptions on top of the error messages given by several database servers.No more trying to decipher cryptic and proprietary SQL error messages! For those who prefer using an object-relational mapping (ORM)tool over straight JDBC.Spring provides the ORM module.Spring's ORM support builds on the DAO sup port,providing a convenient way to build DAOs for several ORM solutions.Spring doesn't attempt to implement its own ORM solution,but does provide hooks into sev- eral popular ORM frameworks,including Hibernate,Java Persistence APL,Java Data Objects,and iBATIS SQL Maps.Spring's transaction management supports each of these ORM frameworks as well as JDBC. We'll see how Spring's template-based JDBC abstraction can greatly simplify JDBC code when we look at Spring data access in chapter 5
22 CHAPTER 1 Springing into action look to other options when Spring doesn’t fit the bill. Spring even offers integration points with several other frameworks and libraries so that you won’t have to write them yourself. Let’s take a look at each of Spring’s modules, one at a time, to see how each fits in the overall Spring picture. CORE SPRING CONTAINER The centerpiece of the Spring Framework is a container that manages how the beans in a Spring-enabled application are created, configured, and managed. Within this module you’ll find the Spring bean factory, which is the portion of Spring that provides dependency injection. Building upon the bean factory, you’ll find several implementations of Spring’s application context, each of which provides a different way to configure Spring. In addition to the bean factory and application context, this module also supplies many enterprise services such as email, JNDI access, EJB integration, and scheduling. As you can see, all of Spring’s modules are built on top of the core container. You’ll implicitly use these classes when you configure your application. We’ll discuss the core module throughout this book, starting in chapter 2 where we dig deep into Spring dependency injection. SPRING’S AOP MODULE Spring provides rich support for aspect-oriented programming in its AOP module. This module serves as the basis for developing your own aspects for your Springenabled application. Like DI, AOP supports loose coupling of application objects. But with AOP, application-wide concerns (such as transactions and security) are decoupled from the objects to which they’re applied. We’ll dig into Spring’s AOP support in chapter 4. DATA ACCESS AND INTEGRATION Working with JDBC often results in a lot of boilerplate code that gets a connection, creates a statement, processes a result set, and then closes the connection. Spring’s JDBC and data access objects (DAO) module abstracts away the boilerplate code so that you can keep your database code clean and simple, and prevents problems that result from a failure to close database resources. This module also builds a layer of meaningful exceptions on top of the error messages given by several database servers. No more trying to decipher cryptic and proprietary SQL error messages! For those who prefer using an object-relational mapping (ORM) tool over straight JDBC, Spring provides the ORM module. Spring’s ORM support builds on the DAO support, providing a convenient way to build DAOs for several ORM solutions. Spring doesn’t attempt to implement its own ORM solution, but does provide hooks into several popular ORM frameworks, including Hibernate, Java Persistence API, Java Data Objects, and iBATIS SQL Maps. Spring’s transaction management supports each of these ORM frameworks as well as JDBC. We’ll see how Spring’s template-based JDBC abstraction can greatly simplify JDBC code when we look at Spring data access in chapter 5. Licensed to Christian Cederquist <chrisman@kaus.dk>
Surveying the Spring landscape 23 This module also includes a Spring abstraction over the Java Message Service (JMS) nally part of the Spring Web Services project. In addition,this module uses Spring's AOP module to provide transaction manage ment services for objects in a Spring application.We'll look at Spring's transaction support in detail in chapter 6. WER AND REMOTING The Model-View-Controller (MVC)paradigm is a commonly accepted approach to build ing web applications such that the user interface is separate from the application logic.Java has no shortage of MVC frameworks,with Apache Struts,ISE,WebWork.and Tapestry among the most popular MVC choices Even though Spring integrates with several popular MVC frameworks,its web and remoting module comes with a capable MVC framework that promotes Spring's loosely coupled techniques in the web layer of an application.This framework comes in two forms:a servlet-based framework for conventional web applications and a portlet-based application for developing against the Java portlet APl. In addition to user-facing web applications,this module also provides several remoting options for building applications that interact with other applications. Spring's reme oting capabilities include Remote Method Invocation (RMD).Hess n,Burlap. JAX-WS,and Spring's own HTTP invoker. We'll look at Spring's MVC framework in chapter 7.Then,in chapter 10,we'll check out Spring remoting TESTING Recognizing the importance of developer-written tests,Spring provides a module ded- icated to testing Spring applications. Within this module you'll find a collection of mock object implementations for writing unit tests against code that works with JNDI,servlets,and portlets.For integra- tion-level testing,this module provides support for loading a collection of beans in a Spring application context and working with the beans in that context We'll get our first taste of Spring's testing module in chapter 4.Then in chapters 5 and 6,we'll expand on what we've learned by seeing how to test Spring data access and transactions 1.3.2 The Spring portfolio When it comes to Spring,there's more than meets the eye.In fact,there's more than what comes in the Spring Framework download.If we stopped at just the core Spring Framework,we'd miss out on a wealth of potential afforded by the larger Spring port folio.The whole Spring portfolio includes several frameworks and libraries that build upon the core Spring Framework and upon each other.All together,the entire Spring portfolio brings the Spring programming model to almost every facet of Java development
Surveying the Spring landscape 23 This module also includes a Spring abstraction over the Java Message Service (JMS) for asynchronous integration with other applications through messaging. And, as of Spring 3.0, this module includes the object-to-XML mapping features that were originally part of the Spring Web Services project. In addition, this module uses Spring’s AOP module to provide transaction management services for objects in a Spring application. We’ll look at Spring’s transaction support in detail in chapter 6. WEB AND REMOTING The Model-View-Controller (MVC) paradigm is a commonly accepted approach to building web applications such that the user interface is separate from the application logic. Java has no shortage of MVC frameworks, with Apache Struts, JSF, WebWork, and Tapestry among the most popular MVC choices. Even though Spring integrates with several popular MVC frameworks, its web and remoting module comes with a capable MVC framework that promotes Spring’s loosely coupled techniques in the web layer of an application. This framework comes in two forms: a servlet-based framework for conventional web applications and a portlet-based application for developing against the Java portlet API. In addition to user-facing web applications, this module also provides several remoting options for building applications that interact with other applications. Spring’s remoting capabilities include Remote Method Invocation (RMI), Hessian, Burlap, JAX-WS, and Spring’s own HTTP invoker. We’ll look at Spring’s MVC framework in chapter 7. Then, in chapter 10, we’ll check out Spring remoting. TESTING Recognizing the importance of developer-written tests, Spring provides a module dedicated to testing Spring applications. Within this module you’ll find a collection of mock object implementations for writing unit tests against code that works with JNDI, servlets, and portlets. For integration-level testing, this module provides support for loading a collection of beans in a Spring application context and working with the beans in that context. We’ll get our first taste of Spring’s testing module in chapter 4. Then in chapters 5 and 6, we’ll expand on what we’ve learned by seeing how to test Spring data access and transactions. 1.3.2 The Spring portfolio When it comes to Spring, there’s more than meets the eye. In fact, there’s more than what comes in the Spring Framework download. If we stopped at just the core Spring Framework, we’d miss out on a wealth of potential afforded by the larger Spring portfolio. The whole Spring portfolio includes several frameworks and libraries that build upon the core Spring Framework and upon each other. All together, the entire Spring portfolio brings the Spring programming model to almost every facet of Java development. Licensed to Christian Cederquist <chrisman@kaus.dk>