IOC (inversion of control) is used to control inversion and dependency (DI) injection

Posted by countcet on Sun, 02 Jan 2022 21:47:27 +0100

Create Maven project

Import dependent POM XML coordinate information:

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

<!--unit testing -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>

Normally create business layer and interface implementation classes, persistence layer interface and implementation classes, and create test classes

Create an XML file with any name (not Chinese) under the root path of the class, and we create bean.xml

according to Official document create configuration document

<?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">
</beans>

Let spring manage resources and configure the service and dao in the configuration file. For example, the following example:

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

    <!--
        bean Label: used to configure spring Create an object and save it to ioc In container
    		id Property: unique identification of the object.
    		class Property: Specifies the fully qualified class name of the object to be created
    -->
    <!-- to configure service -->
    <bean id="accountService" class="com.ywj.service.impl.AccountServiceImpl"></bean>
    
    <!-- to configure dao -->
    <bean id="accountDao" class="com.ywj.dao.impl.AccountDaoImpl"></bean>

</beans>

Get object:

//1. Using the ApplicationContext interface is to obtain the spring container
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2. Get the object according to the id of the bean
        AccountService accountService = (AccountService) ac.getBean("accountService");

ApplicationContext is a container that holds instantiated classes

Relationship and difference between BeanFactory and ApplicationContext

Contact: BeanFactory is the top-level interface in the Spring container. ApplicationContext is its sub interface.
Differences between BeanFactory and ApplicationContext:
Objects are created at different points in time.
ApplicationContext: as soon as the configuration file is read, the object is created by default. (pre created)
BeanFactory: objects are created only when used.

Implementation class of ApplicationContext interface

ClassPathXmlApplicationContext: 
    It loads the configuration file from the root path of the class. This is recommended
FileSystemXmlApplicationContext: 
    It loads the configuration file from the disk path. The configuration file can be anywhere on the disk.
AnnotationConfigApplicationContext:
    When we use annotations to configure container objects, we need to use this class to create spring containers. It is used to read annotations.

bean tags and management object details in IOC

bean tag

effect:
Used to configure the object to be created by spring.
By default, it calls the parameterless constructor in the class. If there is no parameterless constructor, it cannot be created successfully.
Properties:
id: provide a unique identification for the object in the container. Used to get objects.
Class: Specifies the fully qualified class name of the class. Use to create reflective objects. The parameterless constructor is called by default.
Scope: Specifies the scope of the object.
* singleton: default value, single instance
* prototype: multiple cases
* request: in the WEB project, Spring creates a Bean object and stores the object in the request field
* session: in the WEB project, Spring creates a Bean object and stores the object in the session domain
* global session: used in Portlet environment in WEB project If there is no Portlet environment, global session is equivalent to session

Init method: Specifies the name of the initialization method in the class.
Destroy method: Specifies the name of the destroy method in the class.

Scope and lifecycle of bean s

Singleton object: scope="singleton"
An application has only one instance of an object. Its scope is the whole application.
Lifecycle: / / container level
Object birth: when the application loads and creates a container, the object is created.
Object alive: the object is alive as long as the container is.
Object death: when the application unloads and destroys the container, the object is destroyed.

Multi instance object: scope="prototype"
Each time an object is accessed, the object instance is recreated.
Lifecycle: / / use object level
Object birth: creates a new object instance when using an object.
Object alive: as long as the object is in use, it will always be alive.
Object death: when an object is not used for a long time, it is recycled by the java garbage collector.

Three methods of instantiating bean s

spring dependency (DI) implantation

Topics: Java Spring Back-end