SoFunction
Updated on 2025-03-08

Spring Learning to create a project Hello Spring instance code

The main research in this article is the Hello Spring instance code for creating a project for spring learning, as follows.

1. Create an eclipse project and introduce a jar package

1. eclipse creates a java project project HelloSpring

2. Create a lib directory and add 5 jar packages required for spring

3. Select 5 files, right-click -> Build Path -> add to build path

2. Write the hello spring code for spring

1. Create a package and write it

package ;
/**
  * @author ALEX E-mail:zanbin168@
  * @version 1.0
 */
public class HelloWorld {
	private String name;
	public void setName(String name) {
		 = name;
	}
	public void hello() {
		("hello " + name);
	}
}

2. Right-click src -> Create spring bean configuration file

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="/schema/beans" 
  xmlns:xsi="http:///2001/XMLSchema-instance" 
  xsi:schemaLocation="/schema/beans /schema/beans/"> 
   
  <!-- Configurationbean --> 
  <bean  class=""> 
    <property name="name" value="Big Red"></property> 
  </bean> 
 
</beans> 

3. Write

package ;
import ;
import ;
/**
  * @author ALEX E-mail:zanbin168@
  * @version 1.0
 */
public class Main {
	public static void main(String[] args) {
		//1. Create Spring's IOC container object		ApplicationContext ctx = new ClassPathXmlApplicationContext("");
		//2. Get Bean instance from IOC container		HelloWorld helloWorld = (HelloWorld) ("helloWorld");
		//3. Call the hello method		();
	}
}

Output result

When a red spring log is printed in the console, it means that the spring application is successful

Summarize

The above is the full content of this article about the creation project Hello Spring Learning Project Hello Spring instance code. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!