Spring scopes differences

In spring scopes, if I use singleton ,it initialize all beans when it loads the configuration files before executing the logic. But if I use prototype as scope,the bean will be initialized as per the call.Am I right? Does singleton work as static and prototype as instance variable?

MainJava.java:

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainJava {
public static void main(String arg[]){
AbstractApplicationContext applicationContext=new        ClassPathXmlApplicationContext("Beans.xml");
HelloWorld helloWorld=(HelloWorld) applicationContext.getBean("helloworld");
applicationContext.registerShutdownHook();
System.out.println(helloWorld.getMessage());

System.out.println("_________________________");

HelloWorld helloindia=(HelloWorld) applicationContext.getBean("helloindia");
System.out.println(helloindia.getMessage());
helloindia.setMessage("hello vishwa,thisa Bharat");
System.out.println(helloindia.getMessage());
System.out.println("_________________________");

HelloWorld hellonation=(HelloWorld) applicationContext.getBean("helloindia");
System.out.println(hellonation.getMessage());

 }
 }

HelloWorld.java:

     public class HelloWorld {
     private String message;

     public String getMessage() {
     return message;
     }

   public void setMessage(String message) {
   this.message = message;
    }

    public void init(){
    System.out.println("initializing bean");
   }

   public void destroy(){
    System.out.println("destroying bean");
  }
  }

InitHelloWorld.java :

  import org.springframework.beans.BeansException;
  import org.springframework.beans.factory.config.BeanPostProcessor;

  public class InitHelloWord implements BeanPostProcessor{

  @Override
  public Object postProcessAfterInitialization(Object bean, String beanName)     throws BeansException {
    System.out.println("after initalizing the bean "+beanName);
    return bean;
}

     @Override
     public Object postProcessBeforeInitialization(Object bean, String   beanName) throws BeansException {
    System.out.println("before initalizing the bean "+beanName);
    return bean;
   }

   }

Beans.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"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

     <bean name="helloworld" class="com.vishwa.bhat.HelloWorld" init-  method="init" destroy-method="destroy">
  <property name="message" value="hi vishwa,welcome to this world"> </property>
   </bean>

       <bean name="helloindia" class="com.vishwa.bhat.HelloWorld" scope="prototype" init-method="init" destroy-method="destroy">
     <property name="message" value="hi vishwa,welcome to India"></property>
    </bean>
     <bean class="com.vishwa.bhat.InitHelloWord"/>
    </beans>

Output if scope is singleton :

  before initalizing the bean helloworld
  initializing bean
  after initalizing the bean helloworld
  before initalizing the bean helloindia
  initializing bean
  after initalizing the bean helloindia
  hi vishwa,welcome to this world
  _________________________
  hi vishwa,welcome to India
  hello vishwa,thisa Bharat
  _________________________
 hello vishwa,thisa Bharat

 destroying bean
 destroying bean

Output with scope prototype:

 before initalizing the bean helloworld
 initializing bean
after initalizing the bean helloworld
hi vishwa,welcome to this world
_________________________
before initalizing the bean helloindia
initializing bean
after initalizing the bean helloindia
hi vishwa,welcome to India
hello vishwa,thisa Bharat
_________________________
 before initalizing the bean helloindia
 initializing bean
  after initalizing the bean helloindia
 hi vishwa,welcome to India
 destroying bean

The ApplicationContext loads singletons in earger mode by default.

The default behavior for ApplicationContext implementations is to eagerly pre-instantiate all singleton beans at startup. Pre-instantiation means that an ApplicationContext will eagerly create and configure all of its singleton beans as part of its initialization process. Generally this is a good thing, because it means that any errors in the configuration or in the surrounding environment will be discovered immediately (as opposed to possibly hours or even days down the line).

If you need to redefine default behaviour of your singletons just specify lazy-init="true"

链接地址: http://www.djcxy.com/p/82042.html

上一篇: 如何在春季测试中设置环境变量或系统属性?

下一篇: 春天的范围差异