Java Spring on Windows 10 Part 1: Spring Framework
In this part, you shall learn how to install the Spring Framework(download + unzip), configure the Eclipse, and write your first Spring bean. No more chit-chat. It’s cooking time. Let’s start to burn some coffee.
🌿1. Install Spring Framework
🍳2. Setup Eclipse
☕️3. Create a Spring Demo
🌿1. Install Spring Framework
Install Apache Common Logging API
Download the latest version:
On 8/14/2020: commons-logging-1.2-bin.zip
Unzip to “DRIVE:\comlog”. Mine is “D:\comlog”.
In the PATH of Environment Variables, please add “D:\comlog”.
Install Spring Framework
Download the latest version: https://repo.spring.io/release/org/springframework/spring/
On 8/14/2020: 5.2.8.RELEASE\spring-5.2.8.RELEASE-dist.zip
Unzip the file to “DRIVE:\spring”. Mine is “D:\spring”.
In the PATH of Environment Variables, please add “D:\sping\libs”.
🍳2. Setup Eclipse
Let’s open a new Java project.
I don’t need a module yet.
Add Spring Libraries
Right-click your project to choose “Properties Alt+Enter”.
Click: Java Build Path => Libraries => Classpath => Add External JARs
Add commons-logging-1.2:
Try again: Classpath => Add External JARs
Apply and Close.
☕️3. Create a Spring Demo
Let’s add two classes.
Hello.java: prints a message.
public class Hello {
private String msg;
public void setChicken (String s) {
this.msg = s;
}
public void getChicken() {
System.out.println("Message: "+msg);
}
}
SpringApp.java handles the application context.
public class SpringApp {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext(
"com/homanspring/Beans.xml");
Hello mHello =
(Hello) context.getBean("hello");
mHello.getChicken();
}
}
The getBean() links a bean(id=hello) to a Java object. Here, it links to the Hello class.
Bring in the Addon to Ease Your Work.
Let’s add an addon, Spring Tools, to help us to create the Beans.xml.
After the installation, you need to restart Eclipse.
Don’t forget to save the files.
Create Beans.XML
Beans.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 https://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
</beans>
Let’s add a new bean.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=...>
<bean id = "hello" class = "com.homanspring.Hello">
<property name = "chicken" value = "Hello to Spring World!"/>
</bean>
</beans>
Let me show you how do they map each other.
Run.
Message: Hello to Spring World!
Working Fine.