Getting started with SSM spring

Time:2022-5-2

Getting started with SSM spring

Spring framework is an open source J2EE application framework initiated by rod Johnson. It is a lightweight container for managing the life cycle of beans.

Getting to know spring

Start with simple engineering:

  1. Create entity class:

  2. Getting started with SSM spring


  3. Create the spring configuration XML file under the resources directory:
  4. Each bean represents an entity. ID is the alias and class is the full name of the class.


  5. Test:
  6. @Test
    public void student() {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    }

    Classpathxmlapplicationcontext: read the assembly spring XML configuration file.
    The operation results are as follows:
    Getting started with SSM spring
    It can be seen that after assembling the spring XML configuration file, the entity class construction method has been created.

Dependency injection:

IOC (inversion of control) is one of the core ideas of spring framework, and the important implementation method of this idea is di (dependency injection). The principle of dependency injection is to use reflection. In the example of appeal, dependency injection is used to obtain the instance:

  1. Modify the spring configuration file: beans xml
  2. In this configuration file, an entity object is declaredstudent, and its propertiesid,nameAssigned value.

  3. Get the instance through the configuration file
  4. @Test
    public void student() {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student);
    }

    The core method of obtaining an instance is:context.getBean("student"), the principle is that beanfactory obtains through reflection.

  5. View results

  6. Getting started with SSM spring

Using annotations

The above describes how to inject objects using configuration files. Next, let’s see how to annotate objects using annotations:

  1. Modify the spring configuration file: beans xml

  2. Getting started with SSM spring
    To use annotations, you must first add annotation support:
    Next, add the scan package name:

  3. Add notes to entities:

  4. Getting started with SSM spring
    Two notes are used here:@Component @ValueWhere component corresponds toLabel, corresponding to valuelabel
    The entire annotation is equivalent to:

  5. View results

  6. Getting started with SSM spring
    Same effect as using profile injection.

Recommended Today

Introduction to js decorator (introduction to ts decorator)

reference js decorator @Decorator Decorator – Ruan Yifeng TS Decorator(2): Metadata TS Documentation – Decorators hint TypeScript has fully implemented the decorator, and the js decorator is a syntax that is still in the proposal. If you use js instead of ts, you need to configure Babel to use it. The prerequisite knowledge you need […]