春天的范围差异

在Spring范围中,如果我使用singleton,它在执行逻辑之前加载配置文件时初始化所有的bean。 但是如果我使用原型作为范围,那么这个bean会根据这个调用进行初始化。我对吗? 单身人士是否像实例变量一样工作为静态和原型?

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>

如果范围是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

范围原型输出:

 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

ApplicationContext在默认情况下以earger模式加载单例。

ApplicationContext实现的默认行为是在启动时急切地预先实例化所有的singleton bean。 预实例化意味着一个ApplicationContext会急切地创建和配置所有的单例bean作为其初始化过程的一部分。 一般来说,这是一件好事,因为这意味着配置或周围环境中的任何错误都会立即被发现(相对于可能的几小时甚至几天)。

如果你需要重新定义你的单例的默认行为,只需指定lazy-init="true"

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

上一篇: Spring scopes differences

下一篇: Spring contextloaderlistener role in bean creation