Use FactoryBeans
One of the problems that you will face when using Spring is how to create and then inject dependencies
that cannot be created simply by using the new operator. To overcome this problem, Spring provides the
FactoryBean interface that acts as an adaptor for objects that cannot be created and managed using the
standard Spring semantics. Typically, you use FactoryBeans to create beans that you cannot use the new
operator to create such as those you access through static factory methods, although this is not always
the case. Simply put, a FactoryBean is a bean that acts as a factory for other beans. FactoryBeans are
configured within your ApplicationContext like any normal bean, but when Spring uses the FactoryBean
interface to satisfy a dependency or lookup request, it does not return the FactoryBean; instead, it invokes
the FactoryBean.getObject() method and returns the result of that invocation.
FactoryBeans are used to great effect in Spring; the most noticeable uses are the creation of
transactional proxies, which we cover in Chapter 13, and the automatic retrieval of resources from a
JNDI context. However, FactoryBeans are useful not just for building the internals of Spring; you'll find
them really useful when you build your own applications, because they allow you to manage many more
resources using IoC than would otherwise be available.
Custom FactoryBean Example: The MessageDigestFactoryBean
Often the projects that we work on require some kind of cryptographic processing; typically, this involves
generating a message digest or hash of a user's password to be stored in a database. In Java, the
MessageDigest class provides functionality for creating a digest of any arbitrary data. MessageDigest itself
is abstract, and you obtain concrete implementations by calling MessageDigest.getInstance() and
passing in the name of the digest algorithm you want to use. For instance, if we want to use the MD5
algorithm to create a digest, we use the following code to create the MessageDigest instance:
MessageDigest md5 = MessageDigest.getInstance("MD5");
If we want to use Spring to manage the creation of the MessageDigest object, the best we can do
without a FactoryBean is have a property, algorithmName, on our bean and then use an initialization
callback to call MessageDigest.getInstance(). Using a FactoryBean, we can encapsulate this logic inside
a bean. Then any beans that require a MessageDigest instance can simply declare a property,
messageDigest, and use the FactoryBean to obtain the instance. Listing 5-19 shows an implementation of
FactoryBean that does just this.
Listing 5-19. The MessageDigestFactoryBean Class
package com.apress.prospring3.ch5.factory;
import java.security.MessageDigest;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
public class MessageDigestFactoryBean implements
FactoryBean<MessageDigest>, InitializingBean {
private String algorithmName = "MD5";
private MessageDigest messageDigest = null;
public MessageDigest getObject() throws Exception {
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home