Shred Spring5 framework (1) Introduction to Spring5 framework

Overview: Spring is a lightweight, open source JavaEE framework.

Purpose: Solve the complexity of enterprise application development

Core parts: IOC (Inversion of Control) and AOP (Aspect Oriented Programming).

Now that we have talked about the core part, let's talk about all the components of Spring:

The components are as follows:

We can divide the components of Spring into eight categories:

  • Data Access/Integration: data access or data integration, mainly responsible for including the encapsulation of JDBC, object-relational mapping, encapsulation of the conversion between JAVA objects and XML, JAVA message services, etc.

  • Web: The integration of the network module, mainly responsible for network communication, Web-related development, reactive web applications (WebFlux), and the famous Spring MVC are also in this module.

  • AOP: Aspect-Oriented Programming, provides the realization of AOP (Aspect-Oriented Programming)

  • Aspects: Provides integration of the AspectJ framework

  • Instrument: integration module, proxy interface to the server

  • Messaging: Provide support for integrating messaging api and messaging protocol

  • Core Container: Core toolkit, an important part of Spring, including processing of classes, processing of application context, and processing of Spring expressions

  • Test: Simple encapsulation of test frameworks such as JUNIT

Through Spring's integration of such functions, we can summarize the following characteristics of Spring:

Features of Spring:

  • Facilitate decoupling and simplify development

  • AOP programming support

  • Convenient for program testing and integrate Junit

  • Conveniently integrate various other excellent frameworks

  • Declarative transaction support

  • Reduce the difficulty of using JavaEE API

  • Java source code is a classic learning case

Through an introductory case, let us have a better understanding of Spring

1) Environment setup

Create the Mavn project, introduce the coordinates of Spring5, the current version of Spring5: 5.2.8

  • How to reference Spring5 related jar packages?

Visit address: https://github.com/spring-projects/spring-framework/wiki/Spring-Framework-Artifacts

 

From the figure, we can see that there are more than 20 modules in the Spring framework that are managed in different jar packages, so if we want to create a simple spring project, which jar packages should we introduce?

Need to introduce 4 core basic jar packages and a dependent log package common-logging, Junit unit test framework, a total of 6 packages

spring-core
spring-aop
spring-context
spring-expression
common-logging
junit

  • POM introduces jar package
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.learn.spring5</groupId>
    <artifactId>spring5-learn-1</artifactId>
    <version>1.0-SNAPSHOT</version>

        <dependencies>
            <dependency>
            <!--spring-core-->
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>5.2.8.RELEASE</version>
            </dependency>
            <!--spring-beans-->
            <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
            <!--spring上下文包-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.2.8.RELEASE</version>
            </dependency>
                <!--spring表达式包-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-expression</artifactId>
                <version>5.2.8.RELEASE</version>
            </dependency>
                <!--日志包-->
            <dependency>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
                <version>1.2</version>
            </dependency>
            
             <!--Junit单元测试框架-->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
            
        </dependencies>
</project>

The final jar package introduced by the project is as follows:

2) Case demonstration, writing code

1. Create a User class

package org.learn.spring5;

/**
 * User类
 */
public class User {

    /**
     * add方法
     */
    public void add(){
    System.out.println("add...........");
}
}

2. Instantiate an object through Spring's configuration file bean1.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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    
	<!--通过Spring配置User对象-->
    <bean id="user" class="org.learn.spring5.User"></bean>

</beans>

3. Create a test class

package org.learn.spring5;

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

public class TestSpring5 {
    @Test
    public void test() {
        //1.加载spring的配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        //2.获取配置创建的对象
            //第一个参数是配置文件中配置的User对象的ID
        User user = context.getBean("user",User.class);
        System.out.println(user);
        user.add();
    }
}

The execution result of the program

org.learn.spring5.User@2c039ac6
add...........

In summary, we have completed one of the simplest Spring projects. Do we have any knowledge of Spring from the code?

Think about it, after the introduction of the Spring framework, what is the difference from the model we developed before?

By combining the code with the characteristics of Spring described above, can we find that after the introduction of Spring, objects can be easily decoupled. The traditional mode is that we create objects manually by instantiating ourselves. Now we can use Spring configuration files To instantiate the object, and then directly call the method of the object in the code to complete the method execution of the object.

Is it still in a situation where it doesn't know you, and you don't know it well? It doesn't matter. Let's first understand how a project introduces the Spring framework, and have a simple understanding of Sring. After implementing and understanding the convenient operations that Spring brings us, we will continue to learn later.

 

Guess you like

Origin blog.csdn.net/java_cxrs/article/details/108190711