Spring bean scope and life cycle

Spring bean scope and life cycle

 
contain beans
(1) bean factory: the simplest container, providing basic dependency injection support, creating various types of beans
(2) Application Context: Based on the bean factory, it provides system architecture services
 
 
The difference between getting a bean from the ApplicationContext context container and getting a bean from the bean factory container"
 
bean scope

Spring Framework supports five scopes (three of which can only be used in web-based)。

singleton

One bean definition corresponds to one object instance in each Spring IoC container.

prototype

A bean definition corresponds to multiple object instances.

request

In an HTTP request, a bean definition corresponds to an instance; that is, each HTTP request will have its own bean instance, which is created according to a bean definition. This scope is only valid in the context of a web-based Spring  ApplicationContext .

session

In an HTTP  Session , a bean definition corresponds to an instance. This scope is only valid in the context of a web-based Spring  ApplicationContext .

global session

In a global HTTP  Session , a bean definition corresponds to an instance. Typically, this only works when using the portlet context. This scope is only valid in the context of a web-based Spring ApplicationContext .

 
  
 
BeanFactory method (obsolete)
BeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
        UserService userService = (UserService)factory.getBean("userService");
        userService.sayHello();
When instantiating the container in this way, the bean will not be created (lazy, slow, save memory), it will only be loaded when you go to use the bean.
Mobile devices use BeanFactory (saving memory). The other 90% use ApplicationContext
When using ApplicationContext, and when it is a singleton (prototype is not allowed!), the configured bean is instantiated whether you use it or not (early, the advantage is preloading, the disadvantage is memory consumption)
 
 
 
Perhaps the bean's method:
1 ClassPathXmlApplicationContext: Loaded from classpath
2 FileSystemXmlApplicationContext: Loaded from the file system
3 XmlWebApplicationContext: loaded from the web system
 
 
 
 
 
Bean life cycle (scope=singleton as an example)
(1) Instantiate (when the program loads the beans.xml file), instantiate our bean into memory, or use factory-method to call the parameterized constructor
(2) Set properties, provided that there is a setter to succeed
(3) If the bean implements the BeanNameAware interface, the id number can be obtained through setBeanName
(4) If the bean implements the BeanFactoryAware interface, you can obtain the beanFactory
(5) The bean implements the ApplicationContextAware interface, then call setApplicationContext
(6)bean如果和一个后置处理器关联,则会调用两个方法,见下面的程序示例,执行”before“
(7)实现InitializingBean接口,则会调用afterPropertiesSet()方法
(8)调用定制(只有所配置的bean有,非aop)的初始化方法,xml bean里面写init-method
(9)后置处理器的”after“
(10)使用bean
(11)容器关闭
(12)bean实现DisposableBean的destroy()方法关闭数据连接,socket,文件流等
(13)调用定制的销毁方法  xml bean里面写 destroy-method
实际开发中用(1)(2)(6)(9)(10)(11)
BeanFactory 的话少了(5)(6)(9)
 
 
 
 MyBeanPostProcessor.java
copy code
package com.myBeanPostProcessor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor; /** * Created by balfish on 15-3-29. */

class MyBeanPostProcessor implements BeanPostProcessor { @Override
public Object postProcessBeforeInitialization(Object o, String s) throws BeansException { System.out.println("postProcessBeforeInitialization 函数被调用");
   return o;
}
@Override
public Object postProcessAfterInitialization(Object o, String s) throws BeansException { System.out.println("postProcessAfterInitialization 函数被调用");
   return o;
  } }
copy code

 

 

 

applicationContext.xml的变化

 <!--配置自己的后置处理器,类似过滤器-->
    <bean id="myBeanPostProcessor"

class="com.myBeanPostProcessor.MyBeanPostProcessor" />

 

 

应用场景举例

1 记录每个对象实例化的时间

2过滤每个调用ip

3 Add attributes to all objects, or functions (aop for aspect)

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327067664&siteId=291194637