Chapter 5: Beyond the Basics


In the last chapter, we have only really scratched the surface of what the Spring core can do.

Spring provides a wide array of services that supplement and extend the basic IoC capabilities provided by the BeanFactory and associated implementations. A number of projects provide IoC containers, but none so far provides the same comprehensive feature set Spring provides. In this chapter, we are going to look in detail at some additional IoC-related features offered in Spring along with other functionality offered by the Spring core. Specifically, we will be looking at the following:

  • Managing the bean lifecycle: So far all the beans you have seen have been fairly simple and completely decoupled from the Spring container. In this section, we look at some strategies you can employ to enable your beans so they receive notifications from the Spring container at various points throughout their lifecycle. You can do this either by implementing specific interfaces laid out by Spring or by specifying methods that Spring can call via reflection.

  • Making your beans "Spring aware": In some cases, you want a bean to be able to interact with the BeanFactory that configured it. For this reason, Spring offers two interfaces, BeanNameAware and BeanFactoryAware, that allow your bean to obtain its own name and a reference to its BeanFactory, respectively. This section of the chapter looks at implementing these interfaces and some practical considerations for using them in your application.

  • Using method injection: As of release 1.1, Spring provides a new mechanism for managing dependencies when working with beans of different lifecycles (singletons and non-single- tons). In addition to this, Spring provides functionality to allow you to replace any method of any bean with a new implementation, without touching the original code.

  • Using FactoryBeans: As its name implies, the FactoryBean interface is intended to be implemented by any bean that acts as a factory for other beans. The FactoryBean interface provides a mechanism by which you can easily integrate your own factories with the Spring BeanFactory.

  • Working with JavaBeans PropertyEditors: The PropertyEditor interface is a standard interface provided in the java.beans package. PropertyEditors are used to convert property values to and from String representations. Spring uses PropertyEditors extensively, mainly to read values specified in the BeanFactory configuration and convert them into the correct types. In this section of the chapter, we discuss the set of PropertyEditors supplied with Spring and how you can use them within your application. We also take a look at implementing custom PropertyEditors.

  • Learning about the Spring ApplicationContext: The ApplicationContext is an extension of the BeanFactory intended for use in full applications. The ApplicationContext interface provides a useful set of additional functionality, including internationalized message provision, resource loading, and event publishing. ApplicationContext also enables a much simpler configuration and, in many cases, such as when you are building web applications, you can create and configure an ApplicationContext automatically. In this chapter, we take a detailed look at the ApplicationContext and the features it offers. We also jump ahead of ourselves a little and look at how the ApplicationContext simplifies the use of Spring when you are building web applications.

Pro Spring
Pro Spring
ISBN: 1590594614
EAN: 2147483647
Year: 2006
Pages: 189

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: