factory-method: 表示将被调用以注入bean的工厂方法。
factory-bean: 表示将调用工厂方法的bean的引用。如果工厂方法是非静态的,则使用它。
public class A { public static A getA(){//factory method return new A(); } }
<bean id="a" class="com.lidihuo.A" factory-method="getA"></bean>
<bean id="b" class="com.lidihuo.A" factory-method="getB"></bean>
<bean id="a" class="com.lidihuo.A"></bean> <bean id="b" class="com.lidihuo.A" factory-method="getB" factory-bean="a"></bean>
<bean id="a" class="com.lidihuo.A" factory-method="getA"></bean>
A.java
applicationContext.xml
Test.java
package com.lidihuo; public class A { private static final A obj=new A(); private A(){System.out.println("private constructor");} public static A getA(){ System.out.println("factory method "); return obj; } public void msg(){ System.out.println("hello user"); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="a" class="com.lidihuo.A" factory-method="getA"></bean> </beans>
package org.sssit; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); A a=(A)context.getBean("a"); a.msg(); } }
private constructor factory method hello user
Printable.java
A.java
B.java
PrintableFactory.java
applicationContext.xml
Test.java
package com.lidihuo; public interface Printable { void print(); }
package com.lidihuo; public class A implements Printable{ @Override public void print() { System.out.println("hello a"); } }
package com.lidihuo; public class B implements Printable{ @Override public void print() { System.out.println("hello b"); } }
package com.lidihuo; public class PrintableFactory { public static Printable getPrintable(){ //return new B(); return new A();//return any one instance, either A or B } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="p" class="com.lidihuo.PrintableFactory" factory-method="getPrintable"></bean> </beans>
package org.sssit; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); Printable p=(Printable)context.getBean("p"); p.print(); } }
hello a
Printable.java
A.java
B.java
PrintableFactory.java
applicationContext.xml
Test.java
package com.lidihuo; public class PrintableFactory { //non-static factory method public Printable getPrintable(){ return new A();//return any one instance, either A or B } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="pfactory" class="com.lidihuo.PrintableFactory"></bean> <bean id="p" class="com.lidihuo.PrintableFactory" factory-method="getPrintable" factory-bean="pfactory"></bean> </beans>
hello a