Spring autowired prototype scope using JSR 330 Provider

This example shows how to create an autowired bean of prototype scope using JSR 330 Provider API.

PrototypeExample.java (main class)


package com.idodevjobs.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import javax.inject.Provider;

@Service
public class PrototypeExample {

    @Autowired
    private PrototypeService prototypeService;

    @Autowired
    Provider<PrototypeService> prototypeServiceProvider;

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        System.out.println("non-autowired access : " + context.getBean(PrototypeService.class));
        System.out.println("non-autowired access : " + context.getBean(PrototypeService.class));
        PrototypeExample prototypeExample = context.getBean(PrototypeExample.class);
        PrototypeExample prototypeExample1 = context.getBean(PrototypeExample.class);
        prototypeExample.printMessageSingleton();
        prototypeExample1.printMessageSingleton();
        prototypeExample.printMessagePrototype();
        prototypeExample1.printMessagePrototype();
    }

    private void printMessageSingleton() {
        System.out.println("printMessageSingleton : " + prototypeService);
    }

    private void printMessagePrototype() {
        PrototypeService prototypeService1 = prototypeServiceProvider.get();
        System.out.println("printMessagePrototype : " + prototypeService1);
        PrototypeService prototypeService2 = prototypeServiceProvider.get();
        System.out.println("printMessagePrototype : " + prototypeService2);
    }
}

PrototypeService.java (service class)


package com.idodevjobs.example;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

@Service
@Scope("prototype")
public class PrototypeService {
    public String getMessage(String message) {
        return "Hello " + message;
    }
}

spring-config.xml (spring config xml)


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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-4.0.xsd
        http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <context:component-scan base-package="com.idodevjobs.example" />

</beans>

Output


non-autowired access : com.idodevjobs.example.PrototypeService@21f1151f
non-autowired access : com.idodevjobs.example.PrototypeService@5aa1b7a1
printMessageSingleton : com.idodevjobs.example.PrototypeService@2f1ed983
printMessageSingleton : com.idodevjobs.example.PrototypeService@2f1ed983
printMessagePrototype : com.idodevjobs.example.PrototypeService@3a7d7427
printMessagePrototype : com.idodevjobs.example.PrototypeService@3274ae84
printMessagePrototype : com.idodevjobs.example.PrototypeService@a6d75fd
printMessagePrototype : com.idodevjobs.example.PrototypeService@185d3a64

Though the bean is annotated as prototype, whenever an instance is requested, the spring container always gives the same instance. It happens because the service bean is autowired and spring gives one new instance per autowired injection. Whereas in case of non-autowired access, we get a new instance every single time.

To overcome this, we have used JSR 330 Provider API which provides as new instance everytime.

Please leave a comment for any questions or if you find this useful.

Advertisements
This entry was posted in java and tagged , , , . Bookmark the permalink.

3 Responses to Spring autowired prototype scope using JSR 330 Provider

  1. Pingback: Spring Data REST Java Configuration Example | iDoDevJobs

  2. Pingback: Develop a simple RESTful Webservices using Apache CXF and Spring Framework – Part I | iDoDevJobs

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s