SSM Basics - getting started with Spring

Introduction to Spring framework

Overview of Spring framework

Spring is an open source design level framework, which solves the loose coupling problem between business logic layer and other layers. Therefore, it runs through the whole system application with the idea of interface oriented programming.

Spring is a lightweight java development framework rising in 2003, which is derived from some concepts and prototypes elaborated by Rod Johnson in his book expert one on one J2EE development and design. It is created to solve the complexity of enterprise application development. One of the main advantages of the framework is its layered architecture, which allows users to choose which component to use, and provides an integrated framework for Java EE application development.

The core of Spring

The core is control inversion (IOC) and aspect oriented (AOP). Simply put, Spring is a layered Java Se / EE full stack (one-stop) lightweight open source framework.

  1. IOC: inversion of control, leaving the process of creating objects to spring for management
  2. AOP: aspect oriented, code function enhancement without modifying the source code

Advantages of Spring framework

  • It is convenient to decouple and simplify development. Spring is a big factory that can create all objects and maintain dependencies, which is managed by spring. This is also the role of IOC.
  • With the support of AOP programming, Spring provides aspect oriented programming, which can easily realize the functions of permission interception, operation monitoring and so on.
  • The support of declarative transactions can complete the management of transactions through configuration without manual programming.
  • To facilitate program testing, Spring supports Junit4. You can easily test Spring programs through annotations.
  • It is convenient to integrate various excellent frameworks. Spring does not exclude various excellent open source frameworks. It provides direct support for various excellent frameworks (such as struts 2, Hibernate, MyBatis, etc.).
  • Reduce the difficulty of using Java EE APIs. Spring provides encapsulation for some APIs that are very difficult to use in Java EE development (JDBC, JavaMail, etc.), which greatly reduces the difficulty of applying these APIs.

Hello World 

1. Create Maven project

2. Import the dependency and put the following contents in the properties tab in pow In XML

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

3. Create com. In src\main\java directory Qcby package, in which Demo class is created

public class Demo {
    public void hello() {
        System.out.println("hello world");
    }
}

4. Write the configuration file of Spring core and create ApplicationContext in src\main\resources directory XML configuration file, the name can be arbitrary, but generally the default name will be used. The following Spring Config option can only appear after the above dependency import is completed

The contents are 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">
  	 <!--IOC Administration bean-->
    <!--id:Unique identifier of the class class:The full pathname of the class-->
    <bean id="demo" class="com.qcby.service.Demo" />
</beans>

5. Create test class DemoTest in src\test\java directory

public class DemoTest {
        //Traditional writing
        @Test
        public void run(){
            Demo demo = new Demo();
            demo.hello();
        }

        //spring writing
        @Test
        public void run1(){
            //Create a spring factory and load the configuration file
            ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
            //Get bean object
            Demo demo = (Demo) ac.getBean("demo");
            //Call method
            demo.hello();
        }
}

hello world, successfully completed the creation and operation of the first spring. Spring has been successfully introduced.

Keywords: Java JavaEE Spring SSM

Added by usmc on Sun, 20 Feb 2022 21:14:58 +0200