Summer self-study day 16 | spring (II)


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


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""







<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
<!--   Object creation to spring -- >
    <bean id="accountService" class="spring.selflearning.service.impl.AccountServiceImpl"></bean>
    <bean id="accountDao" class="spring.selflearning.dao.impl.AccountDaoImpl"></bean>
  • 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 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);


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>
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