Spring IOC Overview
IOC full name inversion of control, translated as controlled inversion, refers to the way in which objects in a program are acquired in reverse, created from the original new method, and transformed into a third-party framework created, injected (DI), which reduces the coupling between the objects.
The spring container is an implementation of the IOC mechanism, and the IOC is the Foundation and core of the spring framework, which is implemented using the DI (Dependency injection) method.
Spring Bean Container
The Spring container is the core component in the Spring framework, responsible for creating bean objects (a simple specification of Java objects) and managing the dependencies between those objects.
How does the spring container work? When the spring project runs, the spring container is responsible for reading the metadata information in the project, which may be XML-based or annotation-based, and then creating an instance of the Bean object.
IOC is a mechanism in the spring framework that provides control inversion of objects in a container, which is implemented by relying on lookup and dependency injection.
? Spring Container Metadata Configuration
There are two ways to configure metadata in spring, based on XML or annotation. This section understands the basic configuration of the bean elements implemented in XML-based metadata (later in the description annotation mode).
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "Http://www.springframework.org/schema/beans"3 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4 xsi:schemalocation= "Http://www.springframework.org/schema/beans5 http://www.springframework.org/schema/beans/spring-beans.xsd ">6 7 <BeanID="..."class="...">8 <!--collaborators and configuration for this bean go here -9 </Bean>Ten One <BeanID="..."class="..."> A <!--collaborators and configuration for this bean go here - - </Bean> - the <!--More beans definitions go here - - - </Beans>
How to implement multiple element configuration files in spring
1 <Beans>2 <ImportResource= "Services.xml"/>3 <ImportResource= "Resources/messagesource.xml"/>4 <ImportResource= "/resources/themesource.xml"/>5 6 <BeanID= "Bean1"class="..."/>7 <BeanID= "Bean2"class="..."/>8 </Beans>
? Initialization of the spring container
The type of container in spring is the application type, and its initialization method is as follows:
1 ApplicationContext context =2new classpathxmlapplicationcontext ("Beans.xml");
Spring container Initialization is the configuration information that requires the loading of metadata, such as the related configuration of metadata declared in the Beans.xml file.
? use of Spring containers
After the spring container is initialized, it can be passed through the container's Getbean (...). Method gets the Bean object of the container, which in turn operates the Bean object, for example:
1 contex.getbean ("Xxxservice", Xxxservice. Class);
2018-03-10
Writing is limited, Caishuxueqian, if there is a difference in the text, million hope to inform.
Basic application of Spring IOC container