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
IMessageinterface with an abstractdoMessage()method - How to implement the
IMessageinterface in two concrete classes:MessageCNandMessageEN - How to configure the message beans using the
applicationContext.xmlfile - How to test the message switching functionality in the
TestGreetingclass
🏆 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
Create the IMessage Interface
In this step, you will create the IMessage interface, which contains an abstract method called doMessage() without a return value.
- Create a new Java file named
IMessage.javain thecom.labexpackage. - Inside the file, define the
IMessageinterface 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".
- Create a new Java file named
MessageCN.javain thecom.labexpackage. - Inside the file, define the
MessageCNclass 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".
- Create a new Java file named
MessageEN.javain thecom.labexpackage. - Inside the file, define the
MessageENclass 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.
- Create a new file named
applicationContext.xmlin thesrc/main/resourcesdirectory. - 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.
- Create a new Java file named
TestGreeting.javain thecom.labexpackage. - Inside the file, define the
TestGreetingclass 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.
- 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.



