2015-11-26 84 views
3

我是Spring bean的新手,所以我沒有在構造函數arg中使用ref。爲什麼不利用價值再像在這裏這個例子中,java spring bean中constructor-arg中ref的用法是什麼?

這裏是TextEditor.java文件的內容:繼

package com.tutorialspoint; 

public class TextEditor { 
    private SpellChecker spellChecker; 

    public TextEditor(SpellChecker spellChecker) { 
     System.out.println("Inside TextEditor constructor."); 
     this.spellChecker = spellChecker; 
    } 
    public void spellCheck() { 
     spellChecker.checkSpelling(); 
    } 
} 

是其它的從屬類文件SpellChecker.java的內容:

package com.tutorialspoint; 

public class SpellChecker { 
    public SpellChecker(){ 
     System.out.println("Inside SpellChecker constructor."); 
    } 

    public void checkSpelling() { 
     System.out.println("Inside checkSpelling."); 
    } 

} 

以下是MainApp.java文件的內容:以下是

package com.tutorialspoint; 

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

public class MainApp { 
    public static void main(String[] args) { 
     ApplicationContext context = 
      new ClassPathXmlApplicationContext("Beans.xml"); 

     TextEditor te = (TextEditor) context.getBean("textEditor"); 

     te.spellCheck(); 
    } 
} 

它具有配置基於構造注入的配置文件的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"> 

    <!-- Definition for textEditor bean --> 
    <bean id="textEditor" class="com.tutorialspoint.TextEditor"> 
     <constructor-arg ref="spellChecker"/> 
    </bean> 

    <!-- Definition for spellChecker bean --> 
    <bean id="spellChecker" class="com.tutorialspoint.SpellChecker"> 
    </bean> 

</beans> 

在這裏,爲什麼不能做這種方式在哪裏,而不是創建一個bean文本編輯和引用另一個對象的拼寫檢查器,可以直接使用spellChecker bean?

在MainApp.java

TextEditor te = (TextEditor) context.getBean("spellChecker"); 

如果我們使用它,因爲兩個對象在不同的​​班級,這樣我們才能與裁判做掉,如果兩者在同一個班?

此外,如果多個對象引用同一個對象,是否有必要爲每個類創建一個bean,並使用ref來引用這個對象或使用相同的bean,但使用scope = prototype?

+0

你的意思是'SpellChecker sc =(SpellChecker)context.getBean(「spellChecker」);'而不是'TextEditor te =(TextEditor)context.getBean(「spellChecker」);'?在你的例子中,除了[delegate](https://en.wikipedia.org/wiki/Delegation_pattern)任務給SpellChecker之外,TextEditor並沒有做太多的工作。但是,在實際應用中,會有更多的責任。 – asgs

回答

2

Spring bean的ref屬性引用另一個Spring bean實例化的地方。在你的情況下,SpellChecker是一個Spring-singleton bean,你想要注入到另一個TextEditor類型的Spring bean中。 ref很有用的原因是你的大部分bean將會是Singleton,除非你希望每個請求,每個會話等都創建它們。默認範圍是Singleton。

此外,如果多個對象指相同的對象,是有必要 爲每個類創建一個bean,並使用參考來引用此 對象,或使用相同的豆但範圍=原型?

我認爲你想說「多個類是指同一個對象」。在這種情況下,你在類中做的所有事情都是聲明一個對該bean(或對象)的「引用」。你「注入」該bean到依賴的類(或bean)。

你可以閱讀更多關於Spring bean references.

0

此外,如果多個對象是指同一個對象,是有必要爲每個類創建一個bean,並使用裁判指該對象或使用同一個bean但範圍=原型?

如果多個類引用同一個對象,則可以使用單例作用域(默認情況下,如果不指定bean中的scope屬性)。在單例中,多個對象共享單例對象。它們只創建一個單例對象的本地引用。他們不會每次都初始化一個新對象。因此,沒有使用太多的堆內存。而在原型中,每個對象都會創建一個新的內部對象(即在本地初始化一個新對象引用)。因此,創建許多對象會產生開銷,因此堆被加載。

它完全取決於你的應用程序你想使用的範圍。但通常,在Web應用程序中,人們使用Singleton for DAO Connection類,以便連接過程只發生一次,並被所有引用DAOConnection類的類重用。

他們使用POJO類的原型。所以每當類引用它時就會創建一個新對象。

相關問題