Bean Scopes in Spring with Example

Beans in Spring can be either singletons or prototypes. Singleton means that only one instance of the bean will be maintained by the Spring and will be returned every time you call the getBean() method. If you set the scope to prototype, then a new bean will be created every time you call the getBean() method.

The default is singleton. In Java configuration, you can specify that a bean scope is prototype by specifying @Scope("prototype") over the bean definition.

 

Example

Try executing the code, first without the annotation @Scope("prototype") and then using the @Scope("prototype"). In below code I have commented out the annotation for the initial case.

Bean class: JJWriter.java

package com.javajee.spring;

class JJWriter {
    
    private String content="default";
    
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
}

 

Java configuration file: DemoConfig.java

package com.javajee.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Scope;

@Configuration
public class DemoConfig {

    @Bean (name = "jjwriter")
    //@Scope("prototype")
    public JJWriter getJJWriter()
    {
        return new JJWriter();
    }
}

 

Test class: JJWriterWithConfigMain.java

package com.javajee.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class JJWriterWithConfigMain {

    public static void main(String[] args) {

        ApplicationContext context = new AnnotationConfigApplicationContext(DemoConfig.class);

        JJWriter writer1 = context.getBean("jjwriter", JJWriter.class);
        System.out.println("new writer1 content="+writer1.getContent());
        System.out.println("setting writer1 content as: New Content");
        writer1.setContent("New Content");
        System.out.println("writer1 content="+writer1.getContent());
        
        JJWriter writer2 = context.getBean("jjwriter", JJWriter.class);
        System.out.println("new writer2 content="+writer2.getContent());
        System.out.println("setting writer1 content as: New Content 2");
        writer2.setContent("New Content 2");
        System.out.println("writer2 content="+writer2.getContent());
        
        System.out.println("writer1 content="+writer1.getContent());
    }
}

 

Results

If you execute it without specifying any scope, the result will be as follows:

new writer1 content=default
setting writer1 content as: New Content
writer1 content=New Content
new writer2 content=New Content
setting writer1 content as: New Content 2
writer2 content=New Content 2
writer1 content=New Content 2

 

If you execute specifying the scope as prototype, the result will be as follows:

new writer1 content=default
setting writer1 content as: New Content
writer1 content=New Content
new writer2 content=default
setting writer1 content as: New Content 2
writer2 content=New Content 2
writer1 content=New Content

 

Note: In case 1 (singleton) same instance of bean is returned again, whereas in case 2 (prototype) a new instance is created. 

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!