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

2

7

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?

Azotobacter answered 26/11, 2015 at 11:37 Comment(1)
you meant SpellChecker sc = (SpellChecker) context.getBean("spellChecker"); instead of TextEditor te = (TextEditor) context.getBean("spellChecker");? Here in your example, TextEditor doesn't do much except delegate the task to SpellChecker. However, in a real application, there will be more responsibilities.Phina
P
3

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.

Phina answered 26/11, 2015 at 11:49 Comment(0)
E
0

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?

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.

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.

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

Elaterid answered 26/11, 2015 at 13:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.