I am new Springs. In Bean tag I found factory-method and factory-bean Attributes. What is the difference between factory-method and factory-bean?
I am using factory-method to call my getInstance static method to create singleton object.
What is factory-bean used for?
For given replies, What I understood was
Factory-method is used for calling a static method to create object in same bean class.
Factory-bean is used for creating a object based on factory design pattern.
Ex:- I am asking a EggPlant object from VegetableFactory (Which will return vegetable object which was asked)class by passing my vegetable name(EggPlant in this case).
Please correct if I am wrong?
A factory method in the interface defers the object creation to one or more concrete subclasses at run time. The subclasses implement the factory method to select the class whose objects need to be created.
Static factory methods are those that can return the same object type that implements them. Static factory methods are more flexible with the return types as they can also return subtypes and primitives too. Static factory methods are used to encapsulate the object creation process.
Spring provides an option to inject dependency using factory-method along with factory-bean attributes in case of non-static factory methods.
factory-method: represents the factory method that will be invoked to inject the bean. factory-bean: represents the reference of the bean by which factory method will be invoked. It is used if factory method is non-static.
Printable.java
package com.javatpoint; public interface Printable { void print(); }
A.java
package com.javatpoint; public class A implements Printable{ @Override public void print() { System.out.println("hello a"); } }
B.java
package com.javatpoint; public class B implements Printable{ @Override public void print() { System.out.println("hello b"); } }
PrintableFactory.java
package com.javatpoint; public class PrintableFactory { //non-static factory method public Printable getPrintable(){ return new A();//return any one instance, either A or B } }
applicationContext.xml
<?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.javatpoint.PrintableFactory"></bean> <bean id="p" class="com.javatpoint.Printable" factory-method="getPrintable" factory-bean="pfactory"></bean> </beans>
Notice that public Printable getPrintable()
of PrintableFactory.java
is a non-static method. Generally if we want access/call method or other resources of the a class we have to create it's instance. Similarly in that we created it bean. using bean's reference variable pfactory
we are calling the factory method getPrintable
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With