Chinese-English Information Switching via IoC

JavaJavaBeginner
Practice Now

Introduction

In this project, you will learn how to implement a simple Chinese-English message switching system using the Inversion of Control (IoC) design pattern and the Spring framework.

👀 Preview

[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.labex.TestGreeting
你好, Labex
Hello, Labex
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.056 s - in com.labex.TestGreeting
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.295 s
[INFO] Finished at: 2024-05-10T12:05:55Z
[INFO] ------------------------------------------------------------------------

🎯 Tasks

In this project, you will learn:

  • How to create an IMessage interface with an abstract doMessage() method
  • How to implement the IMessage interface in two concrete classes: MessageCN and MessageEN
  • How to configure the message beans using the applicationContext.xml file
  • How to test the message switching functionality in the TestGreeting class

🏆 Achievements

After completing this project, you will be able to:

  • Use the IoC design pattern to decouple the implementation of message functionality from the main application
  • Use the Spring framework to configure and manage the message beans
  • Write unit tests to verify the functionality of the message switching system

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java/ProgrammingTechniquesGroup -.-> java/method_overriding("`Method Overriding`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/interface("`Interface`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/StringManipulationGroup -.-> java/strings("`Strings`") subgraph Lab Skills java/method_overriding -.-> lab-300348{{"`Chinese-English Information Switching via IoC`"}} java/interface -.-> lab-300348{{"`Chinese-English Information Switching via IoC`"}} java/oop -.-> lab-300348{{"`Chinese-English Information Switching via IoC`"}} java/strings -.-> lab-300348{{"`Chinese-English Information Switching via IoC`"}} end

Create the IMessage Interface

In this step, you will create the IMessage interface, which contains an abstract method called doMessage() without a return value.

  1. Create a new Java file named IMessage.java in the com.labex package.
  2. Inside the file, define the IMessage interface with the following code:
package com.labex;

// Interface for message functionality
public interface IMessage {
    // Method signature for displaying a message
    void doMessage();
}

This interface defines the basic structure for any class that wants to implement message functionality. The doMessage() method is an abstract method that will be implemented by the concrete message classes.

Create the MessageCN Class

In this step, you will create the MessageCN class, which implements the IMessage interface. This class will have a msg attribute and provide get/set methods. It will also override the doMessage() method to output the string "你好,Labex".

  1. Create a new Java file named MessageCN.java in the com.labex package.
  2. Inside the file, define the MessageCN class with the following code:
package com.labex;

// Implementation of the IMessage interface for Chinese messages
public class MessageCN implements IMessage {
    private String msg; // Variable to store the message

    // Getter method for retrieving the message
    public String getMsg() {
        return msg;
    }

    // Setter method for setting the message
    public void setMsg(String msg) {
        this.msg = msg;
    }

    // Implementation of the doMessage method from the IMessage interface
    @Override
    public void doMessage() {
        // Print a greeting message in Chinese
        System.out.println("你好, " + msg);
    }
}

The MessageCN class implements the IMessage interface and provides the implementation for the doMessage() method, which prints a greeting message in Chinese.

Create the MessageEN Class

In this step, you will create the MessageEN class, which also implements the IMessage interface. This class will have a msg attribute and provide get/set methods. It will override the doMessage() method to output the string "hello,Labex".

  1. Create a new Java file named MessageEN.java in the com.labex package.
  2. Inside the file, define the MessageEN class with the following code:
package com.labex;

// Implementation of the IMessage interface for English messages
public class MessageEN implements IMessage {
    private String msg; // Variable to store the message

    // Getter method for retrieving the message
    public String getMsg() {
        return msg;
    }

    // Setter method for setting the message
    public void setMsg(String msg) {
        this.msg = msg;
    }

    // Implementation of the doMessage method from the IMessage interface
    @Override
    public void doMessage() {
        // Print a greeting message in English
        System.out.println("Hello, " + msg);
    }
}

The MessageEN class implements the IMessage interface and provides the implementation for the doMessage() method, which prints a greeting message in English.

Create the applicationContext.xml Configuration File

In this step, you will create the applicationContext.xml configuration file to configure the JavaBeans created above and pass the msg attribute information in the configuration.

  1. Create a new file named applicationContext.xml in the src/main/resources directory.
  2. Inside the file, add the following XML configuration:
<?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">

	<!-- Configuration for the Chinese message bean -->
	<bean id="messageCN" class="com.labex.MessageCN">
		<!-- Set the 'msg' property of the MessageCN bean to "Labex" -->
		<property name="msg" value="Labex"/>
	</bean>

	<!-- Configuration for the English message bean -->
	<bean id="messageEN" class="com.labex.MessageEN">
		<!-- Set the 'msg' property of the MessageEN bean to "Labex" -->
		<property name="msg" value="Labex"/>
	</bean>

</beans>

This configuration file sets up the MessageCN and MessageEN beans and initializes their msg properties with the value "Labex".

Create the TestGreeting Class

In this step, you will create the TestGreeting class and add a test method called testGreeting() to perform the test operation.

  1. Create a new Java file named TestGreeting.java in the com.labex package.
  2. Inside the file, define the TestGreeting class with the following code:
package com.labex;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestGreeting {

    @Test
    public void testGreeting() {
        // Load the Spring application context from the XML configuration file
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        // Retrieve the message beans from the context
        IMessage messageCN = (IMessage) context.getBean("messageCN"); // Chinese message bean
        IMessage messageEN = (IMessage) context.getBean("messageEN"); // English message bean

        // Invoke the 'doMessage' method on the Chinese message bean
        messageCN.doMessage();

        // Invoke the 'doMessage' method on the English message bean
        messageEN.doMessage();

        context.close();
    }
}

The TestGreeting class loads the Spring application context from the applicationContext.xml file, retrieves the MessageCN and MessageEN beans, and then calls their doMessage() methods to test the functionality.

Now, you have completed all the steps to create the Chinese-English Information Switching via IoC project. You can now run the test and see the expected output.

  1. Run the test:
cd ~/project/SpringGreeting/
mvn test

The test should pass and you should see the following output:

[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.labex.TestGreeting
你好, Labex
Hello, Labex
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.056 s - in com.labex.TestGreeting
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.295 s
[INFO] Finished at: 2024-05-10T12:05:55Z
[INFO] ------------------------------------------------------------------------

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Java Tutorials you may like