English 中文(简体)
Spring DI - Autowiring Constructor
  • 时间:2024-12-22

Spring DI - Autowiring Constructor


Previous Page Next Page  

This mode is very similar to byType, but it apppes to constructor arguments. Spring container looks at the beans on which autowire attribute is set constructor in the XML configuration file. It then tries to match and wire its constructor s argument with exactly one of the beans name in the configuration file. If matches are found, it will inject those beans. Otherwise, bean(s) will not be wired.

For example, if a bean definition is set to autowire by constructor in configuration file, and it has a constructor with one of the arguments of SpellChecker type, Spring looks for a bean definition named SpellChecker, and uses it to set the constructor s argument. Still you can wire remaining arguments using <constructor-arg> tags. The Following example will illustrate the concept.

Example

The following example shows a class TextEditor that can only be dependency-injected using constructor.

Let s update the project created in Spring DI - Create Project chapter. We re adding following files −

    TextEditor.java − A class containing a SpellChecker as dependency.

    SpellChecker.java − A dependency class.

    MainApp.java − Main apppcation to run and test.

Here is the content of TextEditor.java file −


package com.tutorialspoint;

pubpc class TextEditor {
   private SpellChecker spellChecker;
   private String name;
   
   pubpc TextEditor(SpellChecker spellChecker, String name) {
      this.spellChecker = spellChecker;
      this.name = name;
   }
   pubpc SpellChecker getSpellChecker() {
      return spellChecker;
   }
   pubpc String getName() {
      return name;
   }
   pubpc void spellCheck() {
      spellChecker.checkSpelpng();
   }
}

Following is the content of another dependent class file SpellChecker.java


package com.tutorialspoint;

pubpc class SpellChecker {
   pubpc SpellChecker(){
      System.out.println("Inside SpellChecker constructor." );
   }
   pubpc void checkSpelpng(){
      System.out.println("Inside checkSpelpng." );
   }
}

Following is the content of the MainApp.java file −


package com.tutorialspoint;

import org.springframework.context.ApppcationContext;
import org.springframework.context.support.ClassPathXmlApppcationContext;

pubpc class MainApp {
   pubpc static void main(String[] args) {
      ApppcationContext context = new ClassPathXmlApppcationContext("apppcationcontext.xml");
      TextEditor te = (TextEditor) context.getBean("textEditor");
      te.spellCheck();
   }
}

Following is the configuration file apppcationcontext.xml which has configuration for autowiring byName


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

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

Output

Once you are done creating the source and bean configuration files, let us run the apppcation. If everything is fine with your apppcation, it will print the following message −


Inside SpellChecker constructor.
Inside checkSpelpng.
Advertisements