English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Spring Dependency Injection

Dependency injection using factory method in Spring

The Spring framework provides convenience for injecting beans using the factory method. For this, we can use two attributes of the bean element.

factory-method: indicating the factory method that will be called to inject the bean. factory-bean: indicating the reference to the bean that will call the factory method. If the factory method is non-static, it is used.

A method that returns a class instance is called factory-method.

public class A {
public static A getA(){//factory method
    return new A();
}
}

factory-method type

There can be three types of factory-method:

1)the returned static factory method its own)an instance of the class. Used for the singleton design pattern.

<bean id="a" class="com.w3codebox.A" factory-method="getA"></bean>

2)a type static factory methodit returns anotheran instance of the class. The specific instance used is unknown and decided at runtime.

<bean id="b" class="com.w3codebox.A" factory-method="getB"></bean>

3)a type non-static factorymethod, which returns anotheran instance of the class. The specific instance used is unknown and decided at runtime.

<bean id="a" class="com.w3codebox.A"></bean>
<bean id="b" class="com.w3codebox.A" factory-method="getB" factory-bean="a"></bean>

type1

Let's look at a simple code example to inject dependencies using the static factory method.

<bean id="a" class="com.w3codebox.A" factory-method="getA"></bean>

Let's look at a complete example to inject dependencies using the factory method in Spring. To create this example, we created3a file.

A.java applicationContext.xml Test.java

A.java

This class is a singleton.

package com.w3codebox;
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");
}
}

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="a" class="com.w3codebox.A" factory-method="getA"></bean>
</beans>

Test.java

This class retrieves a Bean from applicationContext.xml and calls the msg method.

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();
}
}

Output:

private constructor
factory method
hello user

type2

Let's look at simple code for injecting dependencies through a static factory method that returns an instance of another class.

To create in this example, we created6a file.

Printable.java A.java B.java PrintableFactory.java applicationContext.xml Test.java

Printable.java

package com.w3codebox;
public interface Printable {
void print();
}

A.java

package com.w3codebox;
public class A implements Printable{
    @Override
    public void print() {
        System.out.println("hello a");
    }
}

B.java

package com.w3codebox;
public class B implements Printable{
    @Override
    public void print() {
        System.out.println("hello b");
    }
}

PrintableFactory.java

package com.w3codebox;
public class PrintableFactory {
    public static Printable getPrintable() {
        //return new B();
              return new A();//Return any instance, 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="p" class="com.w3codebox.PrintableFactory" factory-method="getPrintable"></bean>
</beans>

Test.java

This class gets a Bean from the applicationContext.xml file and calls the print() method.

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();
}
}

Output:

hello a

type3

Let's look at an example of injecting dependencies through a non-static factory method that returns an instance of another class.

To create this example, we have already created6a file.

Printable.java A.java B.java PrintableFactory.java applicationContext.xml Test.java

All files are the same as the previous ones, you only need to change two files: PrintableFactory and applicationContext.xml.

PrintableFactory.java

package com.w3codebox;
public class PrintableFactory {
    //Non-static factory method
    public Printable getPrintable(){
        return new A();//Return any instance, 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.w3codebox.PrintableFactory"></bean>
<bean id="p" class="com.w3codebox.PrintableFactory" factory-method="getPrintable" 
factory-bean="pfactory"></bean>
</beans>

Output:

hello a