Spring (a): Spring IoC entry procedures and preliminary understanding

This article is in accordance with mad God said notes instructional videos to learn, highly recommended, again in layman's language teaching to understand! b Search mad God says or click the link below

https://space.bilibili.com/95256449?spm_id_from=333.788.b_765f7570696e666f.2

 

Spring Profile

  • Spring is a lightweight Inversion of Control (IoC) and the facing section (AOP) of the container frame.

  • Spring framework that is interface21 framework, redesigned, released in March 2004.

  • Author: Rod Johnson

  • Objective: To address the complexity of enterprise development.

  • Idea: to make existing technologies easier to use, in itself is a hodgepodge, the integration of existing technology framework.

Advantage: the Spring is a free open source framework.

  • It is a lightweight, non-invasive framework.

    • Lightweight: small body.

    • Non-invasive: not the impression the original code

  • Inversion of Control (IOC), aspect-oriented programming (AOP)

  • Supports transaction processing, support for frame integration.

 

  • SSH:Struts2 Spring Hibernate

  • SSM:SpringMVC Spring Mybatis

 

Official website: https://spring.io/

Github:https://github.com/spring-projects/spring-framework

maven:https://mvnrepository.com/artifact/org.springframework/spring-webmvc

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.5.RELEASE</version>
</dependency>

 

Spring 7 function modules

 

 

expand

 

 

  • Spring Boot: a rapid development framework

    • Based on Spring Boot can quickly develop a single micro-services.

  • Spring Cloud

    • Spring Cloud is based on Spring Boot achieve.

 

Spring Boot premise of learning, it is necessary to fully grasp the Spring and SpringMVC.

Disadvantages: too long after development, contrary to the original concept, the configuration is very cumbersome.

 

IOC theoretical derivation

  • Inversion of Control, controls reversal.

  • Is an inversion of control design, dependency injection (DI) to achieve a method of IoC.

  • The program does not IoC, we use object-oriented programming, in the program, created by the program control dependencies between objects and objects created entirely hard-coded object.

  • Inversion of Control will create the object transferred to a third party --- way to get dependent objects reversed.

The original writing code, we write:

  1. --- UserDao Interface specification limits the CRUD methods to ensure that other classes can call the correct

public interface UserDao {
    void getUser();
}

 

  1. UserDaoImpl specific implementation class implements --- CRUD methods do not control how other classes implemented

public  class UserDaoImpl the implements UserDao {
     public  void the getUser () { 
        System.out.println ( "Get User Data" ); 
    } 
}

 

  1. --- UserService service interface specification limit business operations to ensure that the interaction class can call the correct

public interface UserService {
    void getUser();
}

 

  1. UserServiceImpl specific method to achieve business class --- business operations, often where calls UserDaoImpl methods to perform database operations

public class UserServiceImpl implements UserService  {
    private UserDao userDao = new UserDaoImpl();
    
    public void getUser() {
        userDao.getUser();
    }
}

 

  1. Interaction Layer --- only need to call the business methods of the class

public  class MyTest01 {
     public  static  void main (String [] args) {
         // user actually calls business layer, dao layer need not be in contact 
        UserServiceImpl that userService = new new UserServiceImpl (); 
        userService.getUser (); 
    } 
}

 

  • In this case, the user's needs may affect our original code, such as the addition of a dao class

public  class UserDaoMysqlImpl the implements UserDao {
     public  void the getUser () { 
        System.out.println ( "Get the MySQL user data" ); 
    } 
}

 


  • This time we need to modify the code UserServiceImpl

public  class UserServiceImpl the implements UserService {
     // after increasing a UserDaoMysqlImpl, where the code must be modified into UserDaoMysqlImpl
     // Private UserDao userDao new new UserDaoImpl = (); 
    Private UserDao userDao = new new UserDaoMysqlImpl ();  
     public  void the getUser () { 
        userDao. the getUser (); 
    } 
}

 

If the program code is large, it will lead to large changes in procedures.

  • If you use a Set interface UserServiceImpl

public  class UserServiceImpl the implements UserService {
     Private UserDao userDao;
     // use dynamic implementation implanting set value 
    public  void setUserDAO (UserDao userDao) {
         the this .userDao = userDao; 
    } 
        public  void the getUser () { 
        userDAO.getUser (); 
    } 
}

 

  • Then the corresponding interaction layer on changed:

public  class MyTest01 {
     public  static  void main (String [] args) {
         // UserServiceImpl new new UserServiceImpl that userService = ();
         // alternating layers selected to create what objects. 
        userService.setUserDao ( new new UserDaoMysqlImpl ()); 
        userService.getUser (); 
    } 
}

 

  • UserServiceImpl original object creation is written dead, it has now become a method set by the customer what objects created by the active choice.

  • This is called inversion of control, on the initiative of the hands of customers.

  • The idea to solve the problem in essence, programmers no longer need to create a managed object. Coupling of the system is greatly reduced. It may be more focused on the realization of new business.

IoC and Spring

  • IoC is the core Spring Framework, using a variety of ways the perfect realization of IoC, you can use the xml configuration, you can also use annotations, a new version of Spring can achieve zero configuration IoC.

  • Spring container during initialization before reading the configuration file, the tissue created objects into a container according to the configuration files and metadata, and then removed from the subject in need IoC container program.

 

 

  • There are two ways to configure Spring Bean:

    • Xml employed configured in a manner definition information and Implementation Bean separation.

    • Annotations using arranged, both can be combined into one, Bean definition information directly to the line release annotation defined in the implementation class, so as to achieve zero-configuration.

  • Inversion control is a description (XML or annotation) and producing a third party or acquired by way of a specific object, to achieve control is inverted in Spring IoC container, which implementation is dependent injection (Dependecy Injection, DI).

 

The sample program

  • beans

package com.rzp.pojo;
​
public class Hello {
​
    private String str;
​
    public String getStr() {
        return str;
    }
​
    public void setStr(String str) {
        this.str = str;
    }
​
    @Override
    public String toString() {
        return "Hello{" +
                "str='" + str + '\'' +
                '}';
    }
}

 

  • beans.xml (Spring recommended name is applicationContext.xml)

<? XML Version = "1.0" encoding = "UTF-. 8"?> 
<Beans xmlns = "http://www.springframework.org/schema/beans" 
       xmlns: the xsi = "http://www.w3.org / 2001 / XMLSchema-instance " 
       xsi: schemaLocation =" http://www.springframework.org/schema/beans 
        HTTPS: // www.springframework.org/schema/beans/spring-beans.xsd "> 
    <-! use Spring to create objects, these have become in Spring bean 
    bean equivalent to a new target ===== the Hello the Hello = new the Hello (); 
    the above mentioned id = variable name
     class = new object 
    property is equivalent to object attributes a setting value
     -> 
    <the bean ID = "Hello"class="com.rzp.pojo.Hello">
        <-! Spring will be assigned using the set method, there is no set method will not work this line -> 
        <Property name = "STR" value = "the Spring" /> 
    </ the bean> </ Beans>

 

  • test

<? XML Version = "1.0" encoding = "UTF-. 8"?> 
<Beans xmlns = "http://www.springframework.org/schema/beans" 
       xmlns: the xsi = "http://www.w3.org / 2001 / XMLSchema-instance " 
       xsi: schemaLocation =" http://www.springframework.org/schema/beans 
        HTTPS: // www.springframework.org/schema/beans/spring-beans.xsd "> 
    <-! use Spring to create objects, these have become in Spring bean 
    bean equivalent to a new target ===== the Hello the Hello = new the Hello (); 
    the above mentioned id = variable name
     class = new object 
    property is equivalent to object attributes a setting value
     -> 
    <the bean ID = "Hello"class="com.rzp.pojo.Hello">
        
        <-! REF: Creating a Spring referenced container object is a good 
        value: specific values, basic data types 
        , if a value, Spring will be assigned using a set method, there is no set method will not work this line -> 
        < name = Property "STR" value = "the Spring" /> 
    </ the bean> </ Beans>

 

 

  • This process is called inversion of control:

  • Control: Object legacy applications is controlled by the program itself was created after using Spring, the object is to create the Spring.

  • Reverse: The program itself does not create objects, but instead passively receiving object.

  • Dependency injection: the method is performed using a set of injection.

  • IOC is a programming idea, changed from active programming passive reception.

  • You can go to look at the underlying source code browsing through newClassPathXmlApplicationContext.

  • The so-called IoC: objects created by the IoC container management, assembly!

 

Sample Program 2

If you use Spring to achieve the original example:

We just need to increase the configuration file beans.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id = "mysqlImpl" class="com.rzp.dao.UserDaoMysqlImpl"/>
    <bean id = "oracleImpl" class="com.rzp.dao.UserDaoOracleImpl"/><bean id="UserServiceImpl" class="com.rzp.service.UserServiceImpl">
        <property name="userDao" ref="mysqlImpl"/>
    </bean></beans>

 

  • Test program no longer needs new object, creating an object is determined by the xml file.

    • This time if you need to modify mysqlImpl to oracleImpl, there is no need to change the code, can modify the xml file by the customer.

import com.rzp.dao.UserDaoImpl;
import com.rzp.dao.UserDaoOracleImpl;
import com.rzp.service.UserServiceImpl;
import javafx.application.Application;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
​
public class MyTest01 {
​
    public static void main(String[] args) {
​
        //获取Spring的容器
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserServiceImpl");
        userServiceImpl.getUser();
​
    }
}
​

 

 

Guess you like

Origin www.cnblogs.com/renzhongpei/p/12616909.html