[SSM hits the big factory directly] Chapter 1: Spring Quick Start

content

1. Introduction to Spring

1.1 What is Spring

1.2 Spring development history

1.3 Advantages of Spring

 1.4 Architecture of Spring

2. Quick Start of Spring

 2.1 Spring development steps

 2.2 Import the basic coordinates of Spring development

2.3 Write Dao interface and implementation class

2.4 Create a Spring core configuration file

2.5 Configure UserDaoImpl in the Spring configuration file

 2.6 Use Spring's API to obtain Bean instances


 

1. Introduction to Spring

1.1 What is Spring

Spring is a layered full-stack lightweight open source framework for Java SE/EE applications, with loC (Inverse Of Control: Inversion of Control) and AOP (Aspect Oriented Programming: Aspect Oriented Programming) as the core.

It provides many enterprise-level application technologies such as presentation layer SpringMvC , persistence layer Spring JDBCTemplate , and business layer transaction management . It can also integrate many famous third-party frameworks and class libraries in the open source world, and gradually become the most used ava EE enterprise application open source framework.

1.2 Spring development history

In 1997, IBM proposed the idea of ​​EJB. In 1998, SUN developed the development standard  specification
EJB1.0 . The father)  Expert One - to - One J2EE Design and Development (2002) expounded the advantages and solutions  of J2EE development and design using EJB The solution (Spring prototype) The latest version of Spring Spring 5.0 general version (GA) was released in September 2017






1.3 Advantages of Spring

1) Convenient decoupling and simplified development
Through the loC container provided by Spring, the dependencies between objects can be controlled by Spring to avoid excessive coupling caused by hard coding. Users no longer have to write code for very low-level requirements such as singleton pattern classes and property file parsing, and can focus more on upper-level applications.

2) AOP programming support
Through the AOP function of Spring, it is convenient to carry out aspect-oriented programming, and many functions that are not easy to be realized by traditional OOP can be easily realized through AOP.

3) The support of declarative transactions
can free us from the monotonous and boring transaction management code, and flexibly manage transactions in a declarative way to improve development efficiency and quality.

4) Convenient program testing
Almost all testing work can be done in a non-container-dependent programming way, and testing is no longer an expensive operation, but something that can be done easily. 

5) It is convenient to integrate various excellent frameworks

Spring's support for various excellent frameworks (Struts, Hibernate, Hessian, Quartz, etc.).

6) Reduce the difficulty of using  JavaEE API Spring has carried out a thin encapsulation layer for JavaEE API (such as JDBC, JavaMail, remote call, etc.), which greatly reduces the difficulty of using these APIs.

7) Java source code is a classic learning example
 . Spring's source code is exquisitely designed, clearly structured, and ingenious. It reflects the master's flexible use of Java design patterns and deep knowledge of Java technology. Its source code is not intended to be the most advanced Java technology. Examples of best practices.

 1.4 Architecture of Spring

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5rGC5LiN6ISx5Y-R,size_20,color_FFFFFF,t_70,g_se,x_16

2. Quick Start of Spring

 2.1 Spring development steps

  1.  Create Maven project
  2. import dependencies
  3. Create Spring empty configuration file
  4. Define the Bean object that needs to be produced
  5. Basic Dependency Injection
  6. Call the Spring factory to create an object

 2.2 Import the basic coordinates of Spring development

<properties>
    <spring.version>5.0.5.RELEASE</spring.version>
</properties>
< dependencies
    <--导入 spring 的 context 坐标, context 依赖 core , beans 、 expression -->
    <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-context</artifactId> 
    <version>$(spring.version)</version>
    </dpendency>
</dependencies>

2.3 Write Dao interface and implementation class

public interface  UserDao {
    public void save();
}

public class UserDaoImpl implements UserDao {
    @Override
    public void save() {
        System.out.println("save running....");//随便编写的输出语句
    }
}

2.4 Create a Spring core configuration file

 Create the applicationContest.xml configuration file on the classpath (resources)

<?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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans     
       http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>

2.5 Configure UserDaoImpl in the Spring configuration file

<?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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans     
       http://www.springframework.org/schema/beans/spring-beans.xsd">
  <bean id="userDao" class="com.project.dao.impl.UserDaoImpl"></bean>
</beans>

 2.6 Use Spring's API to obtain Bean instances

public class UserDaoDemo {

    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserDao userDao = (UserDao) app.getBean("userDao");
        userDao.save();
    }

}

3. Summary

 Spring development steps

  • Import coordinates
  • Create Bean
  • Create applicationContext.xml
  • Configure in config file
  • Create ApplicationContext object getBean

 

 

Guess you like

Origin blog.csdn.net/qq_52360069/article/details/123480910