2017-03-11 12 views
0

スプリングスコープでは、シングルトンを使用すると、ロジックを実行する前に構成ファイルをロードするときにすべての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> 

出力範囲がシングルトンの場合:デフォルトでeargerモードで

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 

答えて

0

ApplicationContextの負荷のシングルトン:スコープのプロトタイプを持つ

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 

出力。 ApplicationContextの実装のための

デフォルトの動作では、起動時に事前にインスタンス化し、すべてのシングルトン豆を熱心にすることです。プリインスタンス化とは、ApplicationContextが初期化プロセスの一環として、すべてのシングルトンBeanを熱心に作成およびコンフィグレーションすることを意味します。一般的には、これは良いことです。これは、設定や周囲の環境のエラーがすぐに検出されることを意味するためです(行の数時間または数日もかからずに)。あなたのシングルトンのデフォルトの動作を再定義する必要がある場合にだけ指定

lazy-init="true"