Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. Why not use value again like in this example here,

Here is the content of TextEditor.java file:

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:

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:

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:

<?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?

In 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?

like image 254
user3248186 Avatar asked Nov 26 '15 11:11

user3248186


People also ask

What is constructor-ARG in Spring?

The constructor-arg element within the bean element is used to set the property value thru constructor injection. Since there is only one constructor in the User bean class, this code will work fine. When there is more than one constructor with the same number of arguments, then the following ambiguities will occur.

What is ref attribute in Spring?

Spring ref attribute The ref attribute is a shortcut to the <ref> tag, used to refer to other beans for injection.

Is used to eliminate property and constructor-arg tag in a bean file?

Using autowiring, it is possible to reduce or eliminate the need to specify properties or constructor arguments, saving a significant amount of typing.In an XmlBeanFactory, the autowire mode for a bean definition is specified by using the autowire attribute of the bean element. The following values are allowed.


1 Answers

The ref attribute of a Spring bean references another Spring bean instantiated somewhere. In your case, SpellChecker is a Spring-singleton bean which you want to "inject" to another Spring bean of type TextEditor. 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.

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?

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).

You can read more about Spring bean references.

like image 64
asgs Avatar answered Nov 15 '22 03:11

asgs