Summer self-study day 16 | spring (II)

Time:2021-12-7

Control reversal (IOC)

If you directly use new to create objects, then applications and resources are directly linked. If the factory mode is used, the factory will provide corresponding object resources to applications, reducing the coupling between programs.

  • It can only reduce the coupling, but not completely eliminate it.
  • The right to create objects is given to the framework*

Control inversion using spring

  • Create a maven project and add dependencies

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>spring.selflearning</groupId>
    <artifactId>day01_eesy_03spring</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.2</version>
        </dependency>
    </dependencies>


</project>

resources/bean.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
        http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--   Object creation to spring -- >
    <bean id="accountService" class="spring.selflearning.service.impl.AccountServiceImpl"></bean>
    <bean id="accountDao" class="spring.selflearning.dao.impl.AccountDaoImpl"></bean>
</beans>
  • The presentation layer uses spring to create an IOC container and obtain the required objects
package spring.selflearning.ui;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import spring.selflearning.dao.IAccountDao;
import spring.selflearning.service.IAccountService;
import spring.selflearning.service.impl.AccountServiceImpl;

/*
Simulate the presentation layer, which is used to call the business layer
 */

/*
Get the IOC core container of spring and get the object according to the ID
 */
public class Client {
    public static void main(String[] args) {

        //Get core container object
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");

        //Get bean object by ID
        IAccountService as = (IAccountService) ac.getBean("accountService");
        IAccountDao adao = ac.getBean("accountDao",IAccountDao.class);
        System.out.println(as);
        System.out.println(adao);
        as.saveAccount();
    }
}

ApplicationContext

Three common implementation classes

  • ClassPathXmlApplicationContext

    • loadClasspathConfiguration file under
    • This is recommended for practical development
  • FileSystemXmlApplicationContext

    • Load the configuration file under any path on the disk
  • AnnotationConfigApplicationContext

    • Create container for reading annotations

The difference between ApplicationContext and beanfactory

  • When ApplicationContext creates a core container, after reading the contents of the configuration fileCreate nowConfiguration object in configuration file
    • Applicable when creating a singleton object
  • When beanfactory creates a core container,Delayed loadingTo create an object, usegetBean()The object is created only when the object is obtained by ID
    • Applicable when creating multiple instances of objects

Three ways to create bean objects

staybean.xmlIn the document, we use<bean></bean>Tag to configure the ID and class properties of the object.
This is the first way:

<bean id="accountService" class="spring.selflearning.service.impl.AccountServiceImpl"></bean>

Directly use this method to configure the properties of the object to be created. There must be a default constructor in the class to which the object belongs, otherwise the object cannot be created normally.

Sometimes there is no default constructor in the built-in jar package of Java or in the class created by others. In this case, we need to use the second method, that is, get the class first, and then use thefactory-beanandfactory-methodProperty to create the desired object.

<!--  This is a simulation factory class, which may exist in a jar package or in a class created by others -- >
<bean id="instanceFactory" class="spring.selflearning.factory.InstanceFactory"></bean>
<!--  Get the corresponding factory and the method corresponding to the object to be created in the factory -- >
<bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"></bean>

The third method is used to create objects of static methods of static factory classes:
classGet static classes from,factory-methodDefine static methods in.

    <bean id="accountService" class = "spring.selflearning.factory.StaticFactory" factory-method="getAccountService"></bean>
</beans>
Other properties of the bean
  • Scope: scope
    • Values: singleton, prototype, request, session, global session
  • Bean object lifecycle
    • Singleton: same as container
    • Prototype: object creation is the beginning. When an object is not used for a long time, it is collected by the Java garbage collector.
  • Inti method: object initialization
  • Destruction method: object destruction

This work adoptsCC agreement, reprint must indicate the author and the link to this article