Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Scopes of beans in Spring

    • 0
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 243
    Comment on it

    Bean Scopes:- Bean have total 5 scopes as follows:-

    singleton:- It can create the single instance per Spring IoC container (default). This is default natur that single instance is stored in a cache and that instance can be stored in entire application, every time when you create the instance it will give the instance of that object from cache and new instance is created.
    Prototype:- Using this scope, a single bean definition to have any number of object instances. It is every time create a new instance of the bean. If the scope is prototype the new instance is created for each request.
    Session:- In this scope a bean definition to an HTTP session. It is valid in the context of a web-based spring application.
    Request:- In this scope a bean definition to an HTTP request. It is valid in the context of a web-based spring application. This indicate the scope is valid only for current request
    Global-session:- In this scope a bean definition to a global HTTP session. It is valid in the context of a web-based springapplication.

    Example:
    Teacher.java

    package com.chandan;
            public class Teacher {
            private String name;
            private int id;
            public String getName() {
                return name;
            }
            public void setName(String name) {
                this.name = name;
            }
            public int getId() {
                return id;
            }
            public void setId(int id) {
                this.id = id;
            }
    
        }
    

    Address.java

      package com.chandan;
    
        public class Address {
    
            private String city;
    
            public String getCity() {
                return city;
            }
    
            public void setCity(String city) {
                this.city = city;
            }
    
    
        }
    

    App.java

    package com.app;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    
    import com.chandan.Teacher;
    
    public class App {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"app-context.xml"});
    
            Teacher teach=(Teacher)ctx.getBean("teach");
    
            //Department dep=emp.getDepartment();
            teach.setId(101);
            teach.setName("Chandan");
            System.out.println("Id:="+teach.getId());
            System.out.println("Name:="+teach.getName());
    
            Teacher teach1=(Teacher)ctx.getBean("teach");
    
            System.out.println("Id:="+teach1.getId());
            System.out.println("Name:="+teach1.getName());
    
    
        }
    
    }
    

    app-context.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:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
       <context:annotation-config/>
    
        <bean id="teach" class="com.chandan.Teacher" scope="singleton">
    
        </bean>
    
        <!--This is default nature -->
    </beans>
    

    output

    Id:=101
    Name:=Chandan
    Id:=101
    Name:=Chandan
    

    Prototype


    app-context.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:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
       <context:annotation-config/>
    
        <bean id="teach" class="com.chandan.Teacher" scope="prototype">
    
        </bean>
    
    
    </beans>
    

    output

        Id:=101
    Name:=Chandan
    Id:=0
    Name:=null
    

    Session

    app-context.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:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
       <context:annotation-config/>
    
        <bean id="teach" class="com.chandan.Teacher" scope="session">
    
        </bean>
    
    
    </beans>
    

    Request

    app-context.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:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
       <context:annotation-config/>
    
        <bean id="teach" class="com.chandan.Teacher" scope="request">
    
        </bean>
    
    
    </beans>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: