简体   繁体   English

在java spring beans中构造函数arg中的ref有什么用?

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

I'm new to spring beans, so I don't get the use of ref in constructor-arg. 我是spring bean的新手,所以我没有在构造函数arg中使用ref。 Why not use value again like in this example here, 为什么不像这个例子那样再次使用价值,

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? 如果我们使用它,因为两个对象都在不同的类中,那么如果两者都在同一个类中,我们可以取消引用吗?

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?

The ref attribute of a Spring bean references another Spring bean instantiated somewhere. Spring bean的ref属性引用了另一个在某处实例化的Spring bean。 In your case, SpellChecker is a Spring-singleton bean which you want to "inject" to another Spring bean of type TextEditor . 在你的情况下, SpellChecker是一个Spring-singleton bean,你想“注入”另一个TextEditor类型的Spring bean。 The reason why ref is 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. ref很有用的原因是你的大多数bean都将是Singleton,除非你希望每个请求,每个会话等创建它们。默认范围是Singleton。

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). 在这种情况下,您在类中所做的只是声明对该bean(或对象)的“引用”。 And you "inject" that bean to the depending classes (or beans). 然后你将“bean”注入到依赖类(或bean)中。

You can read more about Spring bean references. 您可以阅读有关Spring 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?

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). 如果多个类引用同一个对象,则可以使用单例作用域(如果未在bean中指定scope属性,则为默认值)。 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. 因此,使用的堆内存不多。

whereas, in prototype, each and every object creates a new internal object(ie 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. 他们使用原型进行POJO类。 So that a new object is created everytime a class references it. 这样每次类引用它时都会创建一个新对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM