Spring 3.x Enterprise Application Practice-IoC overview, 3. xioc
The concept of IOC in Spring, the concept of control reversal actually contains two levels of meaning, "control" is the control of the choice of interface implementation class; "reverse" means that the control of this choice is transferred from the caller to an external third party or container. BeanFactory, ApplicationContext, and WebApplicationContext are the three core interfaces of the Spring framework. Most of the classes in the framework are centered around them and provide them with support and services. In these support classes, Respurce is an important interface that cannot be ignored. The framework decouples resources from specific resources, regardless of the storage medium in which they are located, can be returned through the same instance. Another interface that works with Resource is ResourceLoader. ResourceLoader adopts the policy mode and can automatically select an appropriate underlying Resource implementation class by passing in the Resource address information, it provides great convenience for the upper layer to reference resources.
I. IOC control reversal:
1. "control" refers to the control of interface implementation class selection
2. "reverse" refers to the transfer of the control right from the call class to the external third-party class or container.
2. Three core interfaces of Spring:
BeanFactory
1. Spring framework infrastructure oriented to Spring itself.
2. the Bean is not instantiated when the container is initialized. The target Bean of the instance is not added until a Bean is accessed for the first time.
3. to define a post-processor for bean, you must manually call the spring method in the Code for registration.
4. Initialization Method new XmlBeanFactory (new Resource ())
ApplicationContext
1. for developers who use the Spring framework, we directly use the Bean defined in the ApplicationContext file in almost all application scenarios.
2. All single-instance beans are instantiated when the application context is initialized.
3. If you need to define a post-Processor for bean, it will automatically identify the Processor in the configuration file using the reflection mechanism and automatically register it in the application context.
4. Initialization Method
New ClassPathXmlApplication ("classpath: bean. xml ")
New FileSystemXmlApplicationContext ("file: beans. xml ")
New AnnotationConfigApplicationContext (Beans. class)
WebApplicationContext
1. The WebApplication needs a ServletContext instance, that is, it must have a Web Container before it can be started.
2. Spring provides Servlet and Listener for starting WebApplicationConetext, which are configured in web. xml.
Org. springframework. web. context. ContextLoaderServlet
Org. springframework. web. context. ContextLoaderListener
3. WebApplicationContext requires the log function
The user can put the Log4j configuration file under the class path WEB-INF/classes, then the Log4j engine can be smoothly started
You can use Log4jConfigServlet and Log4jConfigListener in web. xml to specify the location of the Log4j configuration file, but pay attention to the startup sequence (load-on-startup)
4. Use the java class labeled @ Configuration to provide information, including contextClass and contextConfigLocation.
Iii. Bean lifecycle change methods:
1. The init-method and destroy-method attributes in <bean> specify the initialization and destruction methods for the Bean.
2. InitDestroyAnonotationBeanPostProcessor @ PostConstruct, @ PreDestroy
Mind Map: