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

Spring is based on dependent injection of the set-up function


May 14, 2021 Spring


Table of contents


Spring is based on dependent injection of the set-up function

When a container calls a parameterless constructor or a non-parameter static factory method to initialize your bean, the DI based on the set-up function is done by calling the set-up function on your bean through the container.

Example:

The following example shows a class, TextEditor, that can only use purely value-based injection to implement dependency injection.

Let's work properly with eclipse IDE and follow these steps to create a Spring application.

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

Here's what textEditor .java file:

package com.tutorialspoint;
public class TextEditor {
   private SpellChecker spellChecker;
   // a setter method to inject the dependency.
   public void setSpellChecker(SpellChecker spellChecker) {
      System.out.println("Inside setSpellChecker." );
      this.spellChecker = spellChecker;
   }
   // a getter method to return spellChecker
   public SpellChecker getSpellChecker() {
      return spellChecker;
   }
   public void spellCheck() {
      spellChecker.checkSpelling();
   }
}

Here, you need to check the name conversion of the set-up function method. T o set a variable, spellChecker, we use the setSpellChecker() method, which is very similar to the Java POJO class. Let's create another class-dependent file, SpellChecker.java content:

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's .xml the profile Beans, which has a configuration based on the set-up function 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">
      <property name="spellChecker" ref="spellChecker"/>
   </bean>

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

</beans>

You should be careful to define the difference between a Beans file based on constructor injection .xml set-up function injection. The only difference is that in constructor-based injection, we use the constructor-arg element in the bean tag, and in the value-based function injection, we use the property element in the bean tag.

The second point you need to be aware of is that if you want to pass a reference to an object, you need to use the ref property of the tag, and if you want to pass a value directly, you should use the value property.

When you're done creating the source and bean profiles, let's start running the application. If your application runs well, the following message will be output:

Inside SpellChecker constructor.
Inside setSpellChecker.
Inside checkSpelling.

XML configuration with p-namespace:

If you have many set-up function methods, it is convenient to use p-namespace in an XML profile. Let's take a look at the difference:

Take a standard XML profile with a tag:

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

   <bean id="john-classic" class="com.example.Person">
      <property name="name" value="John Doe"/>
      <property name="spouse" ref="jane"/>
   </bean>

   <bean name="jane" class="com.example.Person">
      <property name="name" value="John Doe"/>
   </bean>

</beans>

The above XML profile can be rewritten in a cleaner way using p-namespace, as follows:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id="john-classic" class="com.example.Person"
      p:name="John Doe"
      p:spouse-ref="jane"/>
   </bean>

   <bean name="jane" class="com.example.Person"
      p:name="John Doe"/>
   </bean>

</beans>

Here, you should not distinguish between specifying the original value and an object reference with p-namespace. The -ref section indicates that this is not a direct value, but a reference to another bean.