Notice that two digest factory beans were defined, one using SHA1 and the other using the default
algorithm. Then for the beans shaDigest and defaultDigest, we instructed Spring to instantiate the
beans by using the corresponding message digest factory bean (factory-bean), and we specified the
method to use to obtain the bean instance (factory-method). Listing 5-26 shows the testing class.
Listing 5-26. MessageDigestFactory in Action
package com.apress.prospring3.ch5.factory;
import org.springframework.context.support.GenericXmlApplicationContext;
public class MessageDigestFactoryExample {
public static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:factory/factoryMethod.xml");
ctx.refresh();
MessageDigester digester = (MessageDigester) ctx.getBean("digester");
digester.digest("Hello World!");
}
}
Running the program will generate the following output:
Using digest1
Using alogrithm: SHA1
[B@877ef83
Using digest2
Using alogrithm: MD5
[B@7a0d85cc
JavaBeans PropertyEditors
For those of you not entirely familiar with JavaBeans concepts, a PropertyEditor is an interface that
converts a property's value to and from its native type representation into a String. Originally, this was
conceived as a way to allow property values to be entered, as String values, into an editor and have them
transformed into the correct type. However, because PropertyEditors are inherently lightweight classes,
they have found uses in many different settings, including Spring.
Because a good portion of property values in a Spring-based application start life in the BeanFactory
configuration file, they are essentially Strings. However, the property that these values are set on may
not be String-typed. So, to save you from having to create a load of String-typed properties artificially,
Spring allows you to define PropertyEditors to manage the conversion of String-based property values
into the correct types.
The Built-in PropertyEditors
As of version 3.1, Spring comes with 13 built-in PropertyEditor implementations that are preregistered
with the BeanFactory. Listing 5-27 shows a simple bean that declares 13 properties, one for each of the
types supported by the built-in PropertyEditors.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home