Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Spring is automatically assembled by the constructor


May 14, 2021 Spring


Table of contents


Spring is automatically assembled by the constructor

This pattern is very similar to byType, but it applies to constructor parameters. T he Spring container is considered beans, and the autowire property of beans is set to constructor in the XML profile. I t then tries to match and wire the parameters of its constructor to one of the beans names in the configuration file. If a match is found, it injects these beans, otherwise it throws an exception.

For example, in a configuration file, if a bean definition is set to be assembled automatically by the constructor, and it has a constructor with one of the arguments of the SpellChecker type, Spring looks for the bean that defines the definition named SpellChecker and uses it to set the parameters of the constructor. Y ou can still connect the remaining properties using the hashtags . The following example illustrates this concept.

Let's use the Eclipse IDE in the right place, and then follow these steps to create a Spring application:

Steps Describe
1 Create a project named SpringExample, and create a package com.tutorialspoint in the src folder of the project you have created.
2 Use the Add External JARs option to add the Spring library you want, as described in the Spring Hello World Example section.
3 Create Java-like TextEditor, SpellChecker, and MainApp in the com.tutorialspoint package.
4 Create Beans' profile Beans in the src .xml.
5 The final step is to create the contents of all Java files and bean profiles and run the application, as explained below.

Here is the contents of .java textEditor file:

package com.tutorialspoint;
public class TextEditor {
   private SpellChecker spellChecker;
   private String name;
   public TextEditor( SpellChecker spellChecker, String name ) {
      this.spellChecker = spellChecker;
      this.name = name;
   }
   public SpellChecker getSpellChecker() {
      return spellChecker;
   }
   public String getName() {
      return name;
   }
   public void spellCheck() {
      spellChecker.checkSpelling();
   }
}

Here's what's in another class-dependent .java SpellChecker:

package com.tutorialspoint;
public class SpellChecker {
   public SpellChecker(){
      System.out.println("Inside SpellChecker constructor." );
   }
   public void checkSpelling()
   {
      System.out.println("Inside checkSpelling." );
   }  
}

Here's what .java mainApp files are:

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();
   }
}

Here is the profile Beans file .xml normal:

<?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" />
      <constructor-arg  value="Generic Text Editor"/>
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
   </bean>

</beans>

However, if you want to use auto-assemble "construct byor," your XML profile will look like this:

<?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" 
      autowire="constructor">
      <constructor-arg value="Generic Text Editor"/>
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="SpellChecker" class="com.tutorialspoint.SpellChecker">
   </bean>

</beans>

Once you're done creating the source code and bean profile, we can run the application. If your application is all right, it will print the following message:

Inside SpellChecker constructor.
Inside checkSpelling.