繁体   English   中英

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

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

我是spring bean的新手,所以我没有在构造函数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 textEditor并引用另一个对象spellChecker,你可以直接使用spellChecker bean吗?

在MainApp.java中

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

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

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

Spring bean的ref属性引用了另一个在某处实例化的Spring bean。 在你的情况下, SpellChecker是一个Spring-singleton bean,你想“注入”另一个TextEditor类型的Spring bean。 ref很有用的原因是你的大多数bean都将是Singleton,除非你希望每个请求,每个会话等创建它们。默认范围是Singleton。

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

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

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

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

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

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

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

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

暂无
暂无

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

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