I. knowledge points
The purpose of the static factory method is to encapsulate the object creation process in the static method.Customers who request an object only need to call this method and do not need to understand the details of the Creation.
Spring supports calling a static factory method to create beans. This factory method should be specified in the factory-method attribute.
Ii. Sample Code
Abstract Product
/** Copyright 2013-2015 */package COM. jackie. codeproject. springrecipesnote. springadvancedioc;/*** title: product. java * product abstract class ** @ author Jackie * @ since APR 14,201 3 8:09:49 * @ version V1.0 */public abstract class product {private string name; private double price; Public Product () {}/*** <p> title: </P> * <p> constructor with parameters </P> * @ Param name * @ Param price */Public Product (string name, double price) {This. name = Name; this. price = price;}/*** @ return the name */Public String getname () {return name ;} /*** @ Param name the name to set */Public void setname (string name) {This. name = Name;}/*** @ return the price */Public double getprice () {return price ;} /*** @ Param price the price to set */Public void setprice (double price) {This. price = price;} Public String tostring () {return name + "" + price ;}}
Battery and disc inherit the product
/** Copyright 2013-2015 */package COM. jackie. codeproject. springrecipesnote. springadvancedioc;/*** title: Battery. java class function description ** @ author Jackie * @ since APR 14,201 3 8:14:26 * @ version V1.0 */public class battery extends product {private Boolean rechargeable; Public battery () {super ();} public battery (string name, double price) {super (name, price);}/*** @ return the rechargeable */Public Boolean isrechargeable () {return rechargeable;}/*** @ Param rechargeable * The Rechargeable to set */Public void setrechargeable (Boolean rechargeable) {This. rechargeable = rechargeable ;}}
/** Copyright 2013-2015 */package COM. jackie. codeproject. springrecipesnote. springadvancedioc;/*** title: disc. java class function description ** @ author Jackie * @ since APR 14,201 3 8:18:30 * @ version V1.0 */public class disc extends product {private int capacity; Public disc () {super ();} public disc (string name, double price) {super (name, price);}/*** @ return the capacity */Public int getcapacity () {return capacity;}/*** @ Param capacity * the capacity to set */Public void setcapacity (INT capacity) {This. capacity = capacity ;}}
Factory productcreator
/** Copyright 2013-2015 */package COM. jackie. codeproject. springrecipesnote. springadvancedioc;/*** title: productcreator. java * factory class ** @ author Jackie * @ since APR 23,201 3 8:37:50 * @ version V1.0 */public class productcreator {public static product createproduct (string productid) {If ("AAA ". equals (productid) {return new battery ("AAA", 2.5);} else if ("cdrw ". equals (productid) {return new disc ("CD-RW", 1.5);} Throw new illegalargumentexception ("unknown product ");}}
To declare a bean created by a static factory method, you must specify the factory class in the class attribute and the factory method name in the factory-method attribute. Finally, use the <constructor-Arg> element to pass the parameters of the method.
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> <bean id="aaa" class="com.jackie.codeproject.springrecipesnote.springadvancedioc.ProductCreator" factory-method="createProduct"> <constructor-arg value="aaa" /> </bean> <bean id="cdrw" class="com.jackie.codeproject.springrecipesnote.springadvancedioc.ProductCreator" factory-method="createProduct"> <constructor-arg value="cdrw" /> </bean></beans>
If the factory method throws any exception, spring will encapsulate it with beancreationexception.
The bean configuration is equivalent to the following code snippet:
Product aaa = ProductCreator.createProduct("aaa"); Product cdrw= ProductCreator.createProduct("cdrw");