Blog Image

Dependency Management With Constructor Injection  

Here we described Spring provided dependency injection to implement "Constructor injection for managing Dependencies between Classes" by using xml approach.

"If we inject Dependent object into the Target class via Constructor then it is called Constructor Injection"

Below is the example for managing Dependencies between Classes via Constructor Injection Using xml

Lets see the example:
Technologies used :-
1) java (1.8).
2) maven (3.5.2).
3) Spring (3.0.0).

Project Structure

Below is the maven project structure with packages and java src files.
Classes
PDFMessageConverter.java
package com.sjg.beans;

public class PDFMessageConverter implements MessageConverter {
    private String messages;

    @Override
    public void convert(String message) {
        this.messages = message;
    }

    @Override
    public String toString() {
        return "PDFMessageConverter [messages=" + messages + "]";
    }
}
HTMLMessageConverter.java
package com.sjg.beans;

public class HTMLMessageConverter implements MessageConverter {
    private String messages;

    @Override
    public void convert(String message) {
        this.messages = message;
    }

    @Override
    public String toString() {
        return "HTMLMessageConverter [messages=" + messages + "]";
    }
}
Interface
MessageConverter.java
package com.sjg.beans;

public interface MessageConverter {
    void convert(String message);
}
Converter.java
package com.sjg.beans;

public class Converter {
    MessageConverter messageConverter = null;

    public Converter(MessageConverter messageConverter) {
        this.messageConverter = messageConverter;
    }

    public void proceed() {
        messageConverter.convert("Message Has Been converted Succesfully...");
    }

    @Override
    public String toString() {
        return "Converter [messageConverter=" + messageConverter + "]";
    }
}
application-context.xml
<?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.xsd">;

    <bean id="pdf" class="com.sjg.beans.PDFMessageConverter" />
    <bean id="html" class="com.sjg.beans.HTMLMessageConverter" />
    <bean id="converter" class="com.sjg.beans.Converter">
        <constructor-arg ref="pdf"></constructor-arg>
    </bean>

</beans>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">;
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.sjg.spring</groupId>
  <artifactId>Dependency-Management-Using-Constructor-Injection</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>Dependency-Management-Using-Constructor-Injection</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>;

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
Test.java
package com.sjg.test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

import com.sjg.beans.Converter;

public class Test {

    public static void main(String[] args) {
        BeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/sjg/common/application-context.xml"));
        Converter converter = factory.getBean("converter", Converter.class);
        converter.proceed();
        System.out.println(converter);

    }
}


Please find attached the Dependency_Management_With_Cons_inj.zip as an maven project as a reference. Feel free to pass your comments in the blog.

About author

User Image
Sriman

A software developer on JDK and JEE platform. I am passionate about Java technology and always an explorer and learner in new technologies in Java. I have experience on Open Source technologies like Struts, Spring, Jsf etc.. and even strong knowledge on Integration/Distributed world like Ejb, Web Services and Restful Services.

2

-Comments

Be the first person to write a comment for this Blog
  • author image
    by:
      Pinkki1
      19-9-2018 05:14:38 AM

    Thank you so much Sriman for sharing , very nice blog.............Its very useful for explaining in the interview..

  • author image
    by:
      Roshya
      07-8-2018 09:16:47 AM

    Thank You ..Its very useful for explaining into interview..

Load More

No More Comments

Leave a Comment

Your comment has been posted and will appear soon.