When you create a bean, the class attribute must be specified, and this is the static factory class. FACTORY-METHOD Specifies the static factory method name.
Interface:
Public interface Being {public void testbeing ();}
Dog class
public class Dog implements Being{private string msg;public void Setmsg (String msg) {this.msg = msg;} @Overridepublic void Testbeing () {System.out.println (msg + "Dog Love Chew Bones");}}
Cat class
public class Cat implements Being{private string msg;public void Setmsg (String msg) {this.msg = msg;} @Overridepublic void Testbeing () {System.out.println (msg + "Cat loves eating Mice!");}}
In Bean Configuration
<?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.0.xsd "><bean id=" dog "class=" Com.springtest2.factory.BeingFactory "factory-method=" getbeing "><constructor-arg value=" dog "/>< Property Name= "MSG" value= "I am a dog"/></bean><bean id= "Cat" class= "Com.springtest2.factory.BeingFactory" Factory-method= "getbeing" ><constructor-arg value= "cat"/><property name= "msg" value= "I am a cat"/></ Bean></beans>
Call Test
private static void Testfactory () {ApplicationContext context = new Classpathxmlapplicationcontext (New string[]{"Beans_ Factory.xml "}); Being B1 = Context.getbean ("Dog", Being.class); b1.testbeing (); Being B2 = Context.getbean ("Cat", Being.class); b2.testbeing ();}
Output results
Spring Learning Summary Creating a bean with a static factory