ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-06-28 09:06:34  阅读:143  来源: 互联网

标签:java spring-mvc spring constructor-injection


我是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.

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(或对象)的“引用”.然后你将“bean”注入到依赖类(或bean)中.

您可以阅读有关Spring bean references.的更多信息

标签:java,spring-mvc,spring,constructor-injection
来源: https://codeday.me/bug/20190628/1313979.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有