Spring范围差异

时间:2017-03-11 07:07:10

标签: java spring

在spring作用域中,如果我使用singleton,它会在执行逻辑之前加载配置文件时初始化所有bean。但是如果我使用prototype作为范围,那么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>

如果范围是单身,则输出:

  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

1 个答案:

答案 0 :(得分:0)

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

  

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

如果您需要重新定义单身人士的默认行为,请指定lazy-init="true"