【问题标题】:Spring scopes differences弹簧范围差异
【发布时间】:2017-03-11 07:07:09
【问题描述】:

在 spring 作用域中,如果我使用单例,它会在执行逻辑之前加载配置文件时初始化所有 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");
  }
  }

初始化HelloWorld.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

【问题讨论】:

    标签: java spring


    【解决方案1】:

    ApplicationContext 默认加载单例模式。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-15
      • 1970-01-01
      • 2021-10-31
      • 2014-03-09
      • 2015-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多