Spring Explanation 1: Introduction and Introduction to Spring

Keywords: Java Spring xml Eclipse Programming

First, what is Spring?

  1. Spring is an open source framework.
  2. Spring came into being to simplify enterprise application development. Using Spring, simple JavaBean s can implement functions previously only EJB can.
  3. Spring is an IOC(DI) and AOP container framework.

2. Specific Description of Spring

  1. Lightweight: Spring is non-intrusive - objects in Spring-based applications can be independent of Spring's API
  2. Dependency injection: (DI-Dependency injection, IOC)
  3. Aspect Oriented Programming (AOP-aspect oriented programming)
  4. Container: Spring is a container because it contains and manages the life cycle of application objects
  5. Framework: Spring implements the use of simple component configurations to compose complex applications that can be combined with XML and java annotations in Spring
  6. One-stop: On the basis of IOC and AOP, open source frameworks and excellent third-party libraries for various enterprise applications can be integrated (in fact Spring itself also provides Spring MVC for presentation layer and Spring JDBC for persistence layer)

3. Building Spring Environment

  1. eclipse installs Spring Tool Suite
    SPRING TOOL SUITE is an Eclipse plug-in, which makes it easier to develop Spring-based applications on Eclipse platform.
  2. Adding package
    Add the following jar packages to the project's classpath:

      

3. Spring configuration files: A typical Spring project needs to create one or more bean configuration files for configuring beans in Spring IOC containers. Bean configuration files can be placed under classpath or in other directories.

4. Establish Spring project and write Hello World:

package com.atguigu.spring.beans;

public class HelloWorld {

    private String name;
    
    public void setName(String name) {
        System.out.println("setName...");
        this.name = name;
    }
    
    public void hello(){
        System.out.println("Hello " + name);
    }
    
    
    public HelloWorld() {
        System.out.println("HelloWorld's construct...");
    }
    
    
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
              http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"
> <bean id="helloworld" class="com.atguigu.spring.beans.HelloWorld"> <property name="name" value="spring"></property> </bean> </beans>
package com.atguigu.spring.beans;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main { public static void main(String[] args) { /* HelloWorld helloWorld = new HelloWorld(); helloWorld.setName("spring"); helloWorld.hello(); */   //1. Create containers ApplicationContext ctx = new ClassPathXmlApplicationContext("appliactionContext.xml");
    //2. Getting bean s from containers HelloWorld hello
= (HelloWorld) ctx.getBean("helloworld");
    //3. Method of calling bean s hello.hello(); } }

5. Test results

Posted by superuser2 on Wed, 20 Mar 2019 21:09:27 -0700