Spring Framework Tutorial (1): How to create a Spring project

One, download Spring5

  • Enter the website spring.io
  • Click project-spring framework on the menu bar
  • Click on spring framework on the left
  • Click on the cat head icon on the right page
  • After entering the page, pull it down and click on the blue link under Access to Binaries
  • After entering the page, pull it down and click on the blue link https://repo.spring.io under Downloading a Distribution.
  • Click the second menu item on the left, and then click release-org-springframework in the list
  • Then copy the second link on the right to form a complete link with https://repo.spring.io/ and visit
  • After jumping, click spring
  • Then choose the spring version you want to download, I chose 5.2.6
  • After entering, click on the file with the suffix of dist.zip to start the download. After downloading, the compressed package will be unlocked

2. Build a project in IDEA

  • After starting IDEA, click to create a new project
  • Click on the Java module, select the JDK version as 1.8, and then the next step
  • Check create project from template, the next step
  • After entering the necessary information, click Finish and wait for the creation

Three, import the relevant jar package

  At present, we focus on the core functions of spring, so we import several related packages such as Beans, Core, Context, and Expression.

  • In the IDEA project, create a new directory lib under spring5_demo1 to store the required jar packages
  • First go to https://search.maven.org/ to download commons-logging-4.0.6.jar for the project log
  • In the unzipped folder, enter the libs folder and find spring-beans-5.2.6.RELEASE, spring-context-5.2.6.RELEASE, spring-core-5.2.6.RELEASE, spring-expression-5.2. 6.RELEASE several jar packages
  • Copy the above 5 jar packages to the lib directory of the project

  Now to import the jar resource

  • In the IDEA project, click File-Project Structure-Module-Dependency, and then click the + sign.
  • Find the location of the 5 jar packages you just copied in, select them, and import them.
  • Click Apply and then click OK.

Fourth, how to create classes and methods

  • Class creation location: under the package name directory under src
  • code show as below:
	package com.wang;
	
	public class User {
    
    
	    public void add(){
    
    
	        System.out.println("add...");
	    }
	}

Five, create a Spring configuration file, and configure the object

  • Create a spring config xml configuration file in the src directory, I named it bean1.xml
  • Configure the class in the xml file as follows:
<?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">

    <!--配置user类-->
    <bean id="user" class="com.wang.User"></bean>

</beans>
  • The following is to write a test class, first create a package test in src for unit testing
  • Then create a new class TestSpring5, the code is as follows:
package com.wang.test;

import com.wang.User;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring5 {
    
    
    @Test	//此注释表明这是一个测试方法
    public void testAdd(){
    
    
        //加载spring配置文件
        ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
        //获取对象
        User user=context.getBean("user",User.class);//第一个user是xml配置中id值

        System.out.println(user);
        user.add();
    }
}

  • Select the method name testAdd in the idea, right-click the mouse, and the option to run this method will appear, select it to run this method. My test results are as follows:

Insert picture description here

Guess you like

Origin blog.csdn.net/Tracycoder/article/details/112481936