spring-IoC容器

2020-03-10  本文已影响0人  格林哈

1. 注入方式

  1. 构造方法注入
    • 对象构造完,马上可以使用
    • 参数过长维护复杂,无法继承,无默认值
  2. setter方法注入
    • 方法可以命名,可以继承,可以有默认值
    • 无法构造完成后,立马使用
    • 可以解决 scope = singleton 的bean 的循环依赖
      • 提前暴露一个单例工厂方法,从而使其他bean能引用到该bean
        • addSingletionFactory
  3. 接口注入
    • 基本不用,强制实现不必要的接口

2. 两种类型的容器

BeanFactory 和 ApplicationContext 继承关系

3. BeanFactory 的对象注册与依赖绑定方式

直接编码

BeanFactory 、 BeanDefinitionRegistry 以及 DefaultListableBeanFactory 的关系

外部配置文件方式

注解方式

4. bean 的 scope (面试:spring bean 支持哪几种scope/作用域 )

<bean id="mockObject2" class="...MockBusinessObject"
scope="prototype"/>

scope 取值

<bean id="requestProcessor" class="...RequestProcessor"
scope="request"/>
<bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>

<bean id="userPreferences" class="com.foo.UserPreferences" scope="globalSession"/>

5. BeanFactoryPostProcessor

6. bean的生命周期

graph TB
实例化bean对象-->设置对象属性
设置对象属性-->检查Aware相关接口并设置相关依赖
检查Aware相关接口并设置相关依赖-->beanPostProcessor前置处理
beanPostProcessor前置处理-->检查是否是InitializingBean以决定是否调用afterPropertiesSet
检查是否是InitializingBean以决定是否调用afterPropertiesSet-->检查是否配置有自定义init-method
检查是否配置有自定义init-method-->beanPostProcessor后置处理
beanPostProcessor后置处理-->注册必要的Destruction相关回调接口
注册必要的Destruction相关回调接口-->使用中
使用中-->是否实现DisPosableBean接口
是否实现DisPosableBean接口-->是否配置有自定义的destory方法

6.1 实例化bean对象,设置对象属性

6.2 检查Aware相关接口并设置相关依赖

package org.springframework.context.support;
class ApplicationContextAwareProcessor implements BeanPostProcessor {
    public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException {
        AccessControlContext acc = null;
        if (System.getSecurityManager() != null && (bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware || bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware || bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware)) {
            acc = this.applicationContext.getBeanFactory().getAccessControlContext();
        }

        if (acc != null) {
            AccessController.doPrivileged(new PrivilegedAction<Object>() {
                public Object run() {
                    ApplicationContextAwareProcessor.this.invokeAwareInterfaces(bean);
                    return null;
                }
            }, acc);
        } else {
            this.invokeAwareInterfaces(bean);
        }

        return bean;
    }
}

6.3 beanPostProcessor

package org.springframework.beans.factory.config;

import org.springframework.beans.BeansException;

public interface BeanPostProcessor {
    Object postProcessBeforeInitialization(Object var1, String var2) throws BeansException;

    Object postProcessAfterInitialization (Object var1, String var2) throws BeansException;
}
    public class PasswordDecodePostProcessor implements BeanPostProcessor {
    public Object postProcessAfterInitialization(Object object, String beanName)
    throws BeansException {
    return object;
    }
    public Object postProcessBeforeInitialization(Object object, String beanName)
    throws BeansException {
    if(object instanceof 我们想要处理的bean.class)
    {
     // 设置属性,加密解密等。
    }
    return object;
    }

}


6.4 InitializingBean,init-method

package org.springframework.beans.factory;
public interface InitializingBean {
    void afterPropertiesSet() throws Exception;
}

6.5 注册必要的Destruction(一次性)相关回调接口

6.6 DisposableBean,destroy-method

package org.springframework.beans.factory;

public interface DisposableBean {
    void destroy() throws Exception;
}
//m中增加一个关闭的钩子,当jvm关闭的时候,会执行系统中已经设置的所有通过方法addShutdownHook添加的钩子,当系统执行完这些钩子后,jvm才会关闭。所以这些钩子可以在jvm关闭的时候进行内存清理、对象销毁等操作。
Runtime.getRuntime().addShutdownHook(Thread hook)
上一篇 下一篇

猜你喜欢

热点阅读