【Spring】00 Getting Started Guide


The Spring framework is the most popular and powerful development framework in the Java field None of them .

Spring framework is the most popular and powerful development framework in the Java field None of them.

Spring framework is the most popular and powerful development framework in the Java field None of them.

Be sure to say important things three times. Throughout history, during its growth, competitors did exist, butits opponents eventually became foils!

1 Introduction

Spring provides us with a wide range of functions, including dependency injection, aspect-oriented programming, data access, transaction management, etc., making the development of Java applications easier and more efficient.

Insert image description here

This article will provide you with aconcise and comprehensive guide to getting started with the Spring framework, purely to be able to quickly grasp the basic concepts and start building own Spring application.

2. Concept

The Spring Framework is a comprehensive Java application development framework that provides comprehensive solutions to enterprise-level application development problems. Core features include Inversion of Control (IoC) and Dependency Injection (DI), making loose coupling between components possible. Next, let us first understand the basic concepts of IoC and DI.

Insert image description here

1) Inversion of Control (IoC)

Inversion of control is a design principle that transfers control of an application from the application code to an external container. In Spring, IoC is embodied in handing over the creation and management of objects to the Spring container instead of manual management by developers. This is achieved using the Spring container, the core of which are beans.

2) Dependency injection (DI)

Dependency injection is an implementation method of IoC. It refers to injecting the dependencies of an object into the object itself, rather than creating or finding dependent objects inside the object. Spring implements IoC through DI to ensure decoupling between objects.

3. Core module

Spring framework consists of multiple modules, each module provides different functions

The following are some of Spring’s core modules:

1)Spring Core

Spring Core modules include IoC containers, Bean definitions, Bean life cycle management, etc.

2)Spring AOP

The Spring AOP module supports aspect-oriented programming, allowing you to define cross-cutting concerns in your application and apply them to your code when needed.

3)Spring MVC

The Spring MVC module is a web framework based on the MVC (Model-View-Controller) design pattern and is used to build web applications.

4)Spring Data

The Spring Data module simplifies data access and supports multiple data stores, including relational databases, NoSQL databases, etc.

5)Spring Boot

Important guests often appear at the finale!

Spring Boot modules are used to simplify the development and deployment of Spring applications. It provides automatic configuration and out-of-the-box features, making it easier to build independent, production-level Spring applications.

4.Write Hello World

Now let us return to the protagonist of this section and use a simple example to show how to use the Spring framework to build a basic application.

1) Create a Maven project

Use IDEA tools Open

First, click File -> New -> Project, enter the project information (Name, etc.), and click the Create button

Insert image description here

Then, open pom.xml and add maven dependency (spring-context). Be sure to click maven's refresh button!

Insert image description here

pom.xml

<dependencies>
   <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-context</artifactId>
       <version>5.3.31</version>
   </dependency>
</dependencies>

2) Write Hello World program

Write a simple Java application that prints Hello World, obtains a bean from the Spring container and calls its print method.

First, right-click on java -> New -> Java Class, enter the name (the package is created directly here), and click Enter

Then, open SpringDemo and add a method to print Hello World

Insert image description here

SpringDemo.java

public class SpringDemo {
    
    
    public void hello() {
    
    
        System.out.println("Hello World");
    }
}

3) Write Spring configuration file

Create a Spring configuration file (for example: applicationContext.xml) and define a simple Bean.

First, right-click on resources -> New -> File , enter the name, and click Enter

Then, open applicationContext.xml and add bean information

Insert image description here

<?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">
    <!-- 定义一个Bean -->
    <bean id="springDemo" class="org.example.cheney.SpringDemo">
    </bean>
</beans>

4) Write a startup program

First, right-click on the org.example.cheney package -> New -> Java Class, enter the name, and click Enter

Then, open the App and add the code to obtain the SpringDemo instance from the Spring container and execute the hello method.

Insert image description here

App.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    
    
    public static void main(String[] args) {
    
    
        String location = "applicationContext.xml";
        ApplicationContext context = new ClassPathXmlApplicationContext(location);
        // 从容器中获取Bean
        SpringDemo springDemo = (SpringDemo) context.getBean("springDemo");
        // 调用Bean的方法
        springDemo.hello();
    }
}

5) Run

Click the little green triangle, then click Run xxx, and finally you will see Hello World on the console.

Insert image description here

Output result:

Hello World

This is just a simple example of getting started with the Spring Framework. As you learn more, you'll be able to leverage the power of Spring to build more complex applications.

Summarize

Through this article, you have understood the core concepts, modules and practical applications of the Spring framework. I also wrote a detailed SpringDemo example to print Hello World. For in-depth study, it is recommended to consult Spring’s official documentation.

Guess you like

Origin blog.csdn.net/yanyc0411/article/details/134917160