The IOC container
IoC is inversion of control, which makes components or classes as loosely coupled as possible. It is the IoC container that creates classes. The Spring container is the core of the Spring framework. The container creates objects, wires them together, configures them, and manages their entire life cycle from creation to destruction. The creation, initialization, and destruction of objects are managed by Spring, rather than by the developer, achieving inversion of control.
IoC is about controlling the dependencies between business objects through containers, rather than the direct manipulation of code in traditional implementations. This is where the concept of “inversion of control” comes in: the transfer of control from application code to an external container, and the transfer of control is called inversion. The benefit of the transfer of control is the reduction of dependencies between business objects.
The Spring container uses dependency injection (DI) to manage the components that make up an application. These objects are called Spring Beans,
The Spring IoC container leverages Java’s POJO classes and configuration metadata to generate a fully configured and executable system or application
Spring describes beans and their dependencies through a configuration file, instantiates beans and establishes dependencies between beans using the Java language’s reflection capabilities.
Spring’s IoC container also provides advanced services such as Bean instance caching, lifecycle management, Bean instance proxy, event publishing, resource loading, etc
The IOC container should be analyzed from the BeanFactory and ApplicationContext interfaces.
BeanFactory
BeanFactory is Spring’s core interface that provides advanced IoC configuration mechanisms. The BeanFactory makes it possible to manage different types of Java objects, and the ApplicationContext is built on top of the BeanFactory. It also provides internationalization support and a framework event system that makes it easier to create real-world applications. The BeanFactory is called the IoC container and the ApplicationContext is called the ApplicationContext. However, the ApplicationContext is sometimes referred to as the Spring container for convenience.
Its main function is to provide support to dependency injection (DI), the container interface in the org. Springframework. Beans. Factory. BeanFactor is defined. BeanFactory and its associated interfaces, such as BeanFactoryAware, DisposableBean, and InitializingBean, remain in Spring, The primary goal is to be backward compatible with existing third-party frameworks that are integrated with Spring to implement IoC control, which can be described as an IoC container that reads javabeans’ definitions from XML configuration files or.properties to implement Javabeans configuration and management creation.
You can use the Bean name through the BeanFactory interface method getBean, so that when retrieving a Bean of type Prototype, you can also generate parameters that specify the constructor for that Prototypebean. This gives you a degree of control over generating prototype-typed beans. With the BeanFactory definition, the user can do the following:
❑ uses the interface method containsBean to enable the user to determine whether the container contains a Bean with the specified name.
❑ uses the interface method isSingleton to check whether the named Bean is of type Singleton. For the Singleton attribute, the user can specify it in the BeanDefinition.
❑ uses the interface method isPrototype to check whether the named Bean is of type Prototype. Like the Singleton property, this property can also be specified by the user in a BeanDefinition.
❑ uses the interface method isTypeMatch to check whether the Class type of the named Bean is a specific Class type. This Class type can be specified by the user.
❑ queries the Class type of the named Bean using the interface method getType.
❑ uses the interface method getAliases to query all aliases of the named Bean defined by the user in the BeanDefinition. These defined interface methods outline the basic characteristics of the IoC container
XmlBeanFactory can read the assembly Javabeans from XML without instantiating any objects when it calls the getBean() method and allocates resource space only when the Javabeans need to be created,
-
The first step is to use the XmlBeanFactory() API provided by the framework to generate the factory beans and the ClassPathResource() API to load the bean configuration files available under the path CLASSPATH. The XmlBeanFactory() API is responsible for creating and initializing all the objects, the beans mentioned in the configuration file.
-
The second step uses the getBean() method of the bean factory object generated in the first step to get the beans you need. This method returns a real object through the bean ID in the configuration file, which can eventually be used for the actual object. Once you have this object, you can use it to call any method
For example, start the Spring IoC container by loading the configuration file via the BeanFactory:
Resource re=new ClassPathResource("applicationContext.xml"); BeanFactory factory=new XmlBeanFactory(re); Test test =factory.getBean("test"); > Configure the following in the XML file: > <! Introduce beans.dtd> > <beans> > <bean id="test" class=" com.test.test "> > </beans>Copy the code
XmlBeanFactory loads the Spring configuration information via Resource and starts the IoC container, which can then be retrieved from the IoC container using the beanFactory.getBean (beanName) method. When the IoC container is started through the BeanFactory, the beans defined in the configuration file are not initialized. The initialization action happens on the first invocation, and for singleton beans, the BeanFactory caches the Bean instance, so the second time you get the Bean using getBean(), you get the Bean instance directly from the IoC container’s cache.
Spring DefaultSingletonBeanRegistry class provides a used to slow the CDS instance Bean cache, it is a HashMap with the cache, a single instance of the Bean beanName as key stored in this HashMap. It is worth mentioning that when you initialize the BeanFactory, you must provide a logging framework for it, using Log4J, that is, providing a Log4J configuration file under the classpath so that you can start the Spring container without an error
ApplicationContext:
ApplicationContext is a higher-level Spring container similar to beanFactory. It can load beans defined in configuration files, cluster all beans together, allocate beans on request, extend the beanFactory container and add internationalization, lifecycle, Events, listeners, provide all the features of BeanFactory and allow users to use more declarative methods. ApplicationContext is derived from BeanFactory and provides more application-oriented functionality. In BeanFactory, many functions need to be implemented programmatically, whereas in ApplicationContext they can be implemented by configuration.
Classes with three implementations:
ClassPathXmlApplicationContext
FileSystemXmlApplicationContext
WebApplicationContext
ClassPathXmlApplicationContext:
Retrieves the configuration file from the current classpath and loads it to create an instance of the container
ApplicationContext context=new ClassPathXmlApplicationContext(String configLocation);
FileSystemXmlApplicationContext:
This class is preferred if the configuration files are placed under the path of the file system. Instead of getting configuration information from the classpath, you can get resources outside the classpath by specifying the location of the configuration file as a parameter, and the container loads defined beans from an XML file.
Here, you need to provide the full path to the constructor XML file
ApplicationContext context=new FileSystemXmlApplicationContext(String configLocation);
WebApplicationContext:
WebApplicationContext is designed for Web applications and allows initialization by loading configuration files from a path relative to the Web root directory. A reference to the ServletContext can be obtained from the WebApplicationContext, and the entire Web application context object will be placed as a property into the ServletContext so that the Web application environment can access the Spring application context. Spring for a special tools WebApplicationContextUtils, through the class getWebApplicationContext ServletContext (sc) method, You can get the WebApplicationContext instance from the ServletContext
There are two methods used in servlets
-
1. Configure the Spring ContextLoaderListener listener in the web. XML servlet.
-
2. Modify web. XML to add a servlet definition to the configuration file using Spring’s ContextLoaderServlert class
There is one important difference between the initialization of the ApplicationContext and the initialization of the BeanFactory. The BeanFactory does not instantiate the Bean when it initializes the container. It does not instantiate the target Bean until it is first accessed. The ApplicationContext instantiates all singleton beans when it initializes the ApplicationContext. Therefore, the ApplicationContext takes a little longer to initialize than the BeanFactory, but later calls have no “first penalty” problem
This article uses the article synchronization assistant to synchronize