Spring Framework provides Spring Factory Method mechanics to create Beans from static(non-static) method of a Factory Class in XML config files. In the tutorial, JavaSampleApproach will introduce Spring Factory Method with sample codes.
Related Posts:
– How to inject Collection in Spring
– Inject Properties from Properties File using Spring Environment
Contents
0. Preparation
Create a POJO bean class & 2 factory classes with static & non-static factory methods for the tutorial:
SampleBean.java
package com.javasampleapproach.factory; public class SampleBean { private String message; public SampleBean(String message){ this.message = message; } public void hello() { System.out.println("Message = " + message); } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
StaticMethodFactory.java
package com.javasampleapproach.factory; public class StaticMethodFactory { public static SampleBean createBean(String message) { return new SampleBean(message); } }
NonStaticMethodFactory.java
package com.javasampleapproach.factory; public class NonStaticMethodFactory { public SampleBean createBean(String message) { return new SampleBean(message); } }
I. Spring Factory Method
Question: How to configure a SampleBean from each factories: StaticMethodFactory & NonStaticMethodFactory?
1. With Java Config
We have a solution with very simple configuration:
JavaConfig.java
package com.javasampleapproach.javaconfig; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.javasampleapproach.factory.NonStaticMethodFactory; import com.javasampleapproach.factory.SampleBean; import com.javasampleapproach.factory.StaticMethodFactory; @Configuration public class JavaConfig { @Bean(name = "bean1") public SampleBean createBeanFromStaticMethodFactory() { return StaticMethodFactory.createBean("This is a Bean created from StaticMethodClass!"); } @Bean(name = "bean2") public SampleBean createBeanFromNonStaticMethodFactory() { NonStaticMethodFactory beanFactory = new NonStaticMethodFactory(); return beanFactory.createBean("This is a Bean created from NonStaticMethodFactory!"); } }
Testing:
Create a simple Application Context file:
package com.javasampleapproach.javaconfig; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.javasampleapproach.factory.SampleBean; public class SpringFactoryMethod { public static void main(String[] args){ ApplicationContext ctx = new AnnotationConfigApplicationContext(JavaConfig.class); SampleBean bean1 = (SampleBean) ctx.getBean("bean1"); SampleBean bean2 = (SampleBean) ctx.getBean("bean2"); bean1.hello(); bean2.hello(); } }
Result:
Message = This is a Bean created from StaticMethodClass! Message = This is a Bean created from NonStaticMethodFactory!
2. Use Spring Factory Method with XML config
Case 1: With Static Method Factory like StaticMethodFactory class.
Use: factory-method
to indicate the method name of the Factory class.
constructor-arg
is used to specify arguments of the factory method.
Case 2: With Non-Static Method Factory like NonStaticMethodFactory class.
Need declare an additional factory bean: NonStaticMethodFactory, then use factory-bean
to indicate it.
Testing: create a simple application context file
package com.javasampleapproach.xmlconfig; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.javasampleapproach.factory.SampleBean; public class SpringFactoryMethod { public static void main(String[] args){ ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); SampleBean bean1 = (SampleBean) ctx.getBean("bean1"); SampleBean bean2 = (SampleBean) ctx.getBean("bean2"); bean1.hello(); bean2.hello(); } }
Result:
Message = This is Bean created from StaticMethodClass! Message = This is Bean created from NonStaticMethodClass!
II. Sourcecode
Last updated on November 14, 2018.
createBeanFromNonStaticMethodFactory is using StaticMethodFactory.
It should be using NonStaticMethodFactory, isnt it ?
I think you are right.
Hi, thank you! It’s my mistake, I’ve just updated! 🙂