Spring is a useful framework for quickly building web applications. I do like it.
Spring requires a Java environment, so you need to install Java first. You can usually download Java java.com(Oracle Java), but I don’t like Oracle (even though it’s still free for developers). I recommend installing OpenJDK instead: https://openjdk.org/install/.
Manual installation of Java:
I don’t like automatic installation, so let me explain manual installation.
Unzip the JDK directory and modify the values of the PATH and JAVA_HOME environment variables. Usually, it’s the path to the “jdk/bin” directory (the bin directory under the JDK installation directory).
On Windows systems, when developing, right-click “My Computer,” select “Properties” -> “Advanced” -> “Environment Variables,” and modify the PATH value to include “jdk/bin.”
On Linux systems, for example, if the JDK is installed in “/usr/local/jdk.”
Add the following two variables:PATH=/usr/local/jdk/bin:$PATH
JAVA_HOME=/usr/local/jdk
Spring download:
You can download the framework from the official website: https://spring.io/.
For beginners, I recommend directly downloading the pre-compiled package (Spring framework is a series of JAR files. By importing the JAR files, you can use Spring):
https://spring.io/projects/spring-framework
You can download it directly from here:
https://repo.spring.io/ui/native/snapshot/org/springframework/spring/
Choose the version that suits you.
Optional (installing Maven for framework building):
This is really useful. I recommend downloading a package from the Maven official website. It’s commonly used in the Spring framework (e.g., Spring Boot, MVC), and you’ll probably need it for future builds. It’s good to be prepared in advance.
For a simple usage of building frameworks with Maven, you can refer to this article I wrote later: https://springblog.one/index/archives/18
Simple usage:
Create a Java project, Spring.
Create a “lib” directory and import the basic Spring packages for development.
Create a JavaBean with a no-argument constructor. It’s needed for Spring’s underlying reflection-based object creation.
package com.springblog.spring.bean;
public class Monster {
private String monsterId;
private String name;
private String skill;
// No-argument constructor: needed for Spring's underlying reflection-based object creation
public Monster() {
}
// Parameterized constructor, setters, getters, toString()
}
In the src directory, create a container configuration file named beans.xml.
Explanation: xmlns represents XML namespace, which is the XML namespace.
<!--
1. Configure the monster object / JavaBean.
2. Multiple beans can be configured within the beans element.
3. bean represents a Java object.
4. class is used to specify the fully qualified class path -> Spring uses reflection to create it (so it should have a no-argument constructor).
5. The id attribute represents the ID of the Java object in the Spring container, and the object can be accessed using this ID.
6. <property name="monsterId" value="100"/> is used to assign a value to the property of the object. If not assigned, the String property will be null.
-->
<bean class="com.springblog.spring.bean.Monster" id="monster01">
<property name="monsterId" value="100"/>
<property name="name" value="OldMan"/>
<property name="skill" value="harpoon "/>
</bean>
- Test
package com.springblog.spring.test;
public class SpringBeanTest {
@Test
public void getMonster() {
// Explanation:
// 1. Create a container ApplicationContext.
// 2. Associate the container with the container configuration file.
// 3. It is common to use the interface type to receive it.
ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
// 3. Get the corresponding object using getBean.
// By default, it returns an Object, but the runtime type is Monster.
// Object monster01 = ioc.getBean("monster01");
Monster monster01 = (Monster) ioc.getBean("monster01");
// 4. Output
System.out.println("monster01" + monster01 + ", monster01 runtime type" + monster01.getClass());
System.out.println("monster01" + monster01 + ", name property=" + monster01.getName() + ", monsterId="+ monster01.getMonsterId());
// 5. It is also possible to specify the Class type directly when getting, to get it again.
Monster monster011 = ioc.getBean("monster01", Monster.class);
System.out.println("monster011=" + monster011);
System.out.println("monster011.name=" + monster011.getName());
System.out.println("ok~~~");
}}
To see which bean objects are injected into the container and output their IDs:
String[] beanDefinitionNames = ioc.getBeanDefinitionNames(); for (String beanDefinitionName : beanDefinitionNames) { System.out.println("beanDefinitionName=" + beanDefinitionName); }
Okay, you have essentially completed the installation of Spring.