Spring learning notes - concepts and IOC - based on xml

I. Spring concepts and learning notes (IOC) - based on xml

concept
  • The core of lightweight java development framework is IOC (inversion of control) and AOP (aspect oriented programming)
  • The function is to decouple, object decouple and module decouple, which can only reduce the coupling
  • Container to place java objects
    • tomcat is also a container: management Servlet, filter and listener
    • web.xml file configuration container
  • Interface oriented:
    • The object relationship is transferred from code to configuration file and container, which is convenient for development and improves flexibility
IOC
  • Instead of maintaining business relationships by yourself, it is managed by Spring container, including putting into container and injection, dependency between objects, creation, and object life cycle managed by Spring

  • Control: object creation, attribute assignment, object lifecycle management

  • Reverse: object permissions are managed by the container

  • Forward conversion: developers use the new construction method to create objects and master the whole process from object creation to destruction

  • realization:

    • Di (Dependency Injection) is the technical implementation of IOC. The program only needs to use objects. The creation and search of objects are implemented inside the container
    • Dependency: ClassA uses the attributes or methods of ClassB, which can only be reduced but cannot be eliminated
  • Through the Spring framework, you only need to provide the object noun to be used to obtain the object corresponding to the name from the container. The Spring underlying uses the reflection mechanism to create the object

  • //Traditional object creation
    SomeService service = new SomeServiceImpl();
    
Spring IOC usage
  • Maven configuration

  • <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.1</version>
    </dependency>
    
  • Spring core file configuration

  • <?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:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            https://www.springframework.org/schema/context/spring-context.xsd">
        <context:component-scan base-package="com.km"/>
    
    </beans>
    
Bean
  • The bean tag is the java object, and the spring managed java object is the bean

  • Properties in bean tag:

    • Class: the fully qualified name of the class managed by spring. It cannot be an interface
    • id is a user-defined name, unique
    • The container managed by spring is a Map. Putting a container is putting a Map
  • Create container object: ApplicationContext, which represents a Spring container

  • //From the bean under the classpath Read the configuration from the XML file and create the container
    ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
    //Get the object and use getBean(id)
    AccountServiceImpl accountService = ac.getBean("accountService", AccountServiceImpl.class);
    
Characteristics of Spring container
  • Spring uses the parameterless construction method by default. By default, no parameterless construction method will lead to object instantiation exceptions
  • When Spring creates objects: by default, all java objects declared by the file will be created when the container is created:
    • Advantages: fast object creation
    • Disadvantages: high memory consumption
    • id as key and object as value
  • You can get the information of the objects in the container through the Spring container object
DI
  • Spring calls the parameterless construction method of the class to create an object, and assigns a value to the attribute after the object is created

  • Attribute assignment method: 1 Tags and attributes in XML configuration file; 2. Notes

  • DI classification: 1 Set injection set; 2. Structural injection

    xml based DI

    set injection:

    • Set value injection: Spring calls the set method in the class to complete attribute assignment. There must be a set method

    • <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
          <property name="driverClassName" value="${jdbc.driver}"/>
          <property name="username" value="${jdbc.username}"/>
          <property name="password" value="${jdbc.password}"/>
          <property name="url" value="${jdbc.url}"/>
          <property name="initialSize" value="${jdbc.initialSize}"/>
          <property name="maxActive" value="${jdbc.maxActive}"/>
          <property name="maxWait" value="${jdbc.maxWait}"/>
      </bean>
      
    • First call the parameterless construction method, and then complete the attribute assignment. At this time, Spring only calls the set method and doesn't care about other contents. As long as there is a set method, you can use set injection

    • Injection of reference type of set:

      • <property name="username" ref="reference type bean of id"/>
        

    Structural injection:

    • There are parameter construction methods in the class called by Spring, which assign values to attributes while creating objects

    • <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
          <constructor-arg name="fairLock" value="true"/>
      </bean>
      
    • Tag of Arg struct:

      • Name: the formal parameter name of the constructor
      • index: parameter attribute of construction method, based on position, starting from 0
      • value: simple type assignment
      • ref: reference type assignment
    • The position order of the constructor Arg tag can also constitute the elements of parameter assignment, so the index can be omitted

    Automatic injection of reference types:

    • Spring can assign values to reference types according to certain rules, which is only valid for reference types

    • Byname (injection by name): the attribute name of the class to be injected corresponds to the id of the bean, provided that the type is consistent

      • <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
            ... ...
        </bean>
        <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" autowire="byName">
            <!--this set Injection can be omitted-->
            <property name="dataSource" ref="dataSource"/>
            <property name="configLocation" value="classpath:MybatisSpringConfig.xml"/>
        </bean>
        
    • Bytype (injection by type): the type of the attribute in the class to be injected corresponds to the class of the bean (interface, inheritance)

      • Spring will automatically compare the required types with all types managed by the container, and automatically inject
    • To resolve the conflict when there are multiple types matching, only one matching type can be reserved when byType

Use of multiple profiles
  • By function module (login, registration...), According to the function of the class (database operation, service...)

  • Use the import tag:

    • <import resource="Path to other configuration files"/>
      
  • Wildcard:

    • <import resource="Spring-*.xml"/>
      
    • All Spring start files will be loaded

    • Note that you cannot include yourself

Tags: Spring

Posted by simon71 on Tue, 03 May 2022 23:36:30 +0300