IOC是控制反转,是一种思想web
DI是依赖注入,是控制翻转的一种实现面试
Spring的IOC容器是用来存放对象(bean)的,Spring传统的xml配置方式,容器的大体加载过程为:spring
一、加载xml配置文件session
二、解析xml文件(BeanDefinitionReder接口的xml解析的实现类)app
三、封装BeanDefinition(对xml或者其余配置文件进行解析,拿到bean的组成信息,例如名称,方法,属性等)ide
四、实例化配置文件中注册的bean(经过反射,经过bean定义的scop属性值例如singleton-默认方式单例,rototype,request,session,肯定bean的存在方式,可是Spring中并非直接实例化,而是经过第5点的Bean工程来管理,从而达到可扩展的目的,保证可扩展,Spring作了不少工做,这部分比较重要)post
五、bean存在容器中(经过反射。BeanFactory是容器根接口,AbtsratAutowireCapableBeanFactory继承自BeanFactory,DafaultListableBeanFactory继承自AbstratAutowireCapableBeanFactory。这三个类在Spring中大量出现和使用)this
从反射到bean容器,为了保证扩展性,须要作不少加强处理,加强处理的接口是BeanFactoryPostProcessor和BeanPostProcessor,这两个接口有大量的实现。PostProcessor命名常被称做加强器。能够理解为BeanPostProcessor是为了对bean信息进行加强和修改,BeanFactoryPostProcessor是对beanDefibition进行加强和修改。BeanPostProcessor有两个方法,一个前置处理方法,一个后置处理方法code
例如PlaceholderConfigurerSupport的父类PropertyResourceConfigurer实现了BeanFactoryPostProcessor。那么PlaceholderConfigurerSupport能够动态修改bean的定义信息xml
(1)面试题:能详细描述下bean的生命周期么?(重要)
BeanFactor接口定义了Bean生命周期前后顺序,解释以下:
* <p>Bean factory implementations should support the standard bean lifecycle interfaces * as far as possible. The full set of initialization methods and their standard order is: * <ol> * <li>BeanNameAware's {@code setBeanName} * <li>BeanClassLoaderAware's {@code setBeanClassLoader} * <li>BeanFactoryAware's {@code setBeanFactory} * <li>EnvironmentAware's {@code setEnvironment} * <li>EmbeddedValueResolverAware's {@code setEmbeddedValueResolver} * <li>ResourceLoaderAware's {@code setResourceLoader} * (only applicable when running in an application context) * <li>ApplicationEventPublisherAware's {@code setApplicationEventPublisher} * (only applicable when running in an application context) * <li>MessageSourceAware's {@code setMessageSource} * (only applicable when running in an application context) * <li>ApplicationContextAware's {@code setApplicationContext} * (only applicable when running in an application context) * <li>ServletContextAware's {@code setServletContext} * (only applicable when running in a web application context) * <li>{@code postProcessBeforeInitialization} methods of BeanPostProcessors * <li>InitializingBean's {@code afterPropertiesSet} * <li>a custom init-method definition * <li>{@code postProcessAfterInitialization} methods of BeanPostProcessors * </ol>
(2)面试题:Aware接口到底有什么做用?
经过一种回调的方式,能够拿到Spring的各类组件。好比我建立的A对象,原来只能拿到A对象拿到A对象相应的信息,可是我想经过A对象,拿到Spring容器的其余各类对象,那么能够借助这个借口实现。
Aware借口介绍以下,Aere有不少实现类和实现接口。
package org.springframework.beans.factory; /** * A marker superinterface indicating that a bean is eligible to be notified by the * Spring container of a particular framework object through a callback-style method. * The actual method signature is determined by individual subinterfaces but should * typically consist of just one void-returning method that accepts a single argument. * * <p>Note that merely implementing {@link Aware} provides no default functionality. * Rather, processing must be done explicitly, for example in a * {@link org.springframework.beans.factory.config.BeanPostProcessor}. * Refer to {@link org.springframework.context.support.ApplicationContextAwareProcessor} * for an example of processing specific {@code *Aware} interface callbacks. * * @author Chris Beams * @author Juergen Hoeller * @since 3.1 */ public interface Aware { }
例如咱们想经过咱们建立的A对象拿到beanName。那么咱们能够在A的实体类上实现BeanAware接口,实现接口方法,在咱们实例化A对象后,咱们能够经过A实例,拿到beanNanem
import org.springframework.beans.factory.BeanNameAware; public class A implements BeanNameAware { private String beanName; @Override public void setBeanName(String name) { this.beanName = name; } public String getBeanName() { return beanName; } }
容器底层是用map结构来支持的,因此咱们能够经过容器get出咱们想要的对象bean。Spring中大体存在(String,Object),(Class, Object),(String, ObjectFactory),(String,BeanDefinition)几种类型的map
(3)面试题:Spring内置对象和Spring普通对象有什么区别?
内置对象是Spring提早初始化的,是容器须要的对象,好比beanFactory等,并非咱们经过配置文件注册的bean。Spring普通对象是咱们经过xml或者配置类注册进Spring容器的咱们须要的对象。
(4)面试题:BeanFactory和FactorBean的区别?
import com.dtsre.dashboard.Student; import org.springframework.beans.factory.FactoryBean; public class StudentFactoryBeanTest implements FactoryBean<Student> { @Override public Student getObject() throws Exception { return new Student(); } @Override public Class<?> getObjectType() { return Student.class; } }
把该FactoryBean的实例StudentFactoryBeanTest注册到Spring容器中,以后经过拿到容器context,拿到StudentFactoryBeanTest(getBean(StudentFactoryBeanTest)),只有geeBean操做后,Student对象才会被建立