java spring bean的constructor-arg中的ref有什么用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33937683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:18:54  来源:igfitidea点击:

What is the use of ref in constructor-arg in java spring beans?

javaspringspring-mvcconstructor-injection

提问by user3248186

I'm new to spring beans, so I don't get the use of ref in constructor-arg. Why not use value again like in this example here,

我是 spring bean 的新手,所以我没有在构造函数参数中使用 ref。为什么不像这里的这个例子那样再次使用 value,

Here is the content of TextEditor.java file:

下面是 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();
   }
}

Following is the content of another dependent class file SpellChecker.java:

以下是另一个依赖类文件 SpellChecker.java 的内容:

package com.tutorialspoint;

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

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

}

Following is the content of the MainApp.java file:

以下是 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();
   }
}

Following is the configuration file Beans.xml which has configuration for the constructor-based injection:

以下是配置文件 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>

Here, why not do it this way where, instead of creating a bean textEditor and referencing another object spellChecker, you can directly use the spellChecker bean?

在这里,为什么不这样做,而不是创建 bean textEditor 并引用另一个对象 SpellChecker,而是直接使用 SpellChecker bean?

In MainApp.java

在 MainApp.java 中

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

If we use it because both the objects are in different classes, then can we do away with ref if both are in same class?

如果我们因为两个对象在不同的​​类中而使用它,那么如果两个对象在同一个类中,我们可以取消 ref 吗?

Also if multiple objects refer to same object, is it necessary to create a bean for each of those classes and use ref to refer to this object or use same bean but with scope=prototype?

此外,如果多个对象引用同一个对象,是否有必要为每个类创建一个 bean 并使用 ref 来引用这个对象或使用相同的 bean 但使用 scope=prototype?

采纳答案by asgs

The refattribute of a Spring bean references another Spring bean instantiated somewhere. In your case, SpellCheckeris a Spring-singleton bean which you want to "inject" to another Spring bean of type TextEditor. The reason why refis useful is most of your beans are going to be Singleton unless you want them to be created per request, per session, etc., The default scope is Singleton.

refSpring bean的属性引用在某处实例化的另一个 Spring bean。在您的情况下,SpellChecker是一个 Spring-singleton bean,您想将它“注入”到另一个类型为 的 Spring bean TextEditorref有用的原因是您的大多数 bean 将是单例的,除非您希望它们按请求、每个会话等创建,默认范围是单例。

Also if multiple objects refer to same object, is it necessary to create a bean for each of those classes and use ref to refer to this object or use same bean but with scope=prototype?

此外,如果多个对象引用同一个对象,是否有必要为每个类创建一个 bean 并使用 ref 来引用这个对象或使用相同的 bean 但使用 scope=prototype?

I take it that you wanted to say "multiple classes refer to same object". In that case, all you are doing in your classes is declare a "reference" to that bean (or object). And you "inject" that bean to the depending classes (or beans).

我认为您想说“多个类引用同一个对象”。在这种情况下,您在类中所做的就是声明对该 bean(或对象)的“引用”。然后将该 bean“注入”到依赖的类(或 bean)中。

You can read more about Spring bean references.

您可以阅读有关Spring bean 引用的更多信息

回答by Tanveer Dayan

Also if multiple objects refer to same object, is it necessary to create a bean for each of those classes and use ref to refer to this object or use same bean but with scope=prototype?

此外,如果多个对象引用同一个对象,是否有必要为每个类创建一个 bean 并使用 ref 来引用这个对象或使用相同的 bean 但使用 scope=prototype?

If multiple classes refer to the same object, you can use the singleton scope(default if you dont specify the scope attribute in a bean). In singleton, multiple objects share the singleton object.They only create a local reference to the singleton object. They do not initialize a new object everytime. Hence, not much Heap memory is used.

如果多个类引用同一个对象,则可以使用单例作用域(如果不在 bean 中指定作用域属性,则为默认值)。在单例中,多个对象共享单例对象。它们只创建对单例对象的本地引用。他们不会每次都初始化一个新对象。因此,使用的堆内存并不多。

whereas, in prototype, each and every object creates a new internal object(i.e initializes a new object reference locally). Therefore, there is overhead of creating many objects, as a result, the heap is loaded.

而在原型中,每个对象都会创建一个新的内部对象(即在本地初始化一个新的对象引用)。因此,创建许多对象会产生开销,因此会加载堆。

It completely depends on you application about which scope you would like to use. But normally, in web applications, people use Singleton for DAO Connection classes so that the connection process occurs just once and is reused by all the classes referencing the DAOConnection class.

这完全取决于您想要使用哪个范围的应用程序。但通常,在 Web 应用程序中,人们将 Singleton 用于 DAO Connection 类,以便连接过程只发生一次,并被引用 DAOConnection 类的所有类重用。

They use prototype for POJO classes. So that a new object is created everytime a class references it.

他们使用 POJO 类的原型。这样每次类引用它时都会创建一个新对象。