01-什么是Spring?
Spring是一个轻量级框架,然后你们百度之后会有很多听不懂名词,但是这些都不重要,重要的是你要理解Spring可以给你日常开发带来什么样的改变,也就是
02-Spring用来做什么的?
Spring是用来为我们管理对象的,他给我们带来的改变是,我们不再需要手动的创建对象了,我们只需要使用配置或者注解进行配置就可以了。
03-Spring的基本原理机制什么?
这个是面试必问,也是常见难点,甚至一些面试官喜欢问:“说一下Spring?”
是不是觉得很广泛,不知道怎么说起,根据我长达两个月的面试经验,只要说到这些我觉得就没问题了:
1,Spring的基本实现机制是IOC和AOP
2,我们一般叫Spring叫做容器,用来盛放对象的容器,这个容器的作用就是用来管理我们Spring的对象
04-什么IOC(Inversion of Control)?
IOC全程是:Inversion of Control,
中文名字叫做:控制反转。
它实现了,我们创建对象,由普通的手动new,改变为Spring为我们创建对象。
那么我们怎么去理解这个IOC全流程呢?
从使用角度上讲,使用的入口就是ApplicationContext。
1,入口:
查看源码相关注释:
package org.springframework.context;
import org.springframework.beans.factory.HierarchicalBeanFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.core.io.support.ResourcePatternResolver;
/**
* Central interface to provide configuration for an application.
* This is read-only while the application is running, but may be
* reloaded if the implementation supports this.
*
* 翻译:中央接口配置为应用程序提供。这是只读的,而应用程序正在运行,但如果实现支持这种可能重新加载
*
* <p>An ApplicationContext provides:
* <ul>
* <li>Bean factory methods for accessing application components.
* Inherited from {@link org.springframework.beans.factory.ListableBeanFactory}.
* <li>The ability to load file resources in a generic fashion.
* Inherited from the {@link org.springframework.core.io.ResourceLoader} interface.
* <li>The ability to publish events to registered listeners.
* Inherited from the {@link ApplicationEventPublisher} interface.
* <li>The ability to resolve messages, supporting internationalization.
* Inherited from the {@link MessageSource} interface.
* <li>Inheritance from a parent context. Definitions in a descendant context
* will always take priority. This means, for example, that a single parent
* context can be used by an entire web application, while each servlet has
* its own child context that is independent of that of any other servlet.
* </ul>
*
* 翻译:
* <p>ApplicationContext 提供:
* <ul>
* <li>用于访问应用程序组件的Bean 工厂方法。
* 继承自 {@link org.springframework.beans.factory.ListableBeanFactory}。
* <li>以通用方式加载文件资源的能力。
* 继承自 {@link org.springframework.core.io.ResourceLoader} 接口。
* <li>能够将事件发布到注册的侦听器。
* 继承自 {@link ApplicationEventPublisher} 接口。
* <li>能够解析消息,支持国际化。
* 继承自 {@link MessageSource} 接口。
* <li>从父上下文继承。 后代上下文中的定义
* 将始终优先。 这意味着,例如,单个父上下文可以被整个 Web 应用程序使用,而每个 servlet 都有自己的子上下文,该子上下文独立于任何其他 servlet 的子上下文。
* < /ul>
*
* <p>In addition to standard {@link org.springframework.beans.factory.BeanFactory}
* lifecycle capabilities, ApplicationContext implementations detect and invoke
* {@link ApplicationContextAware} beans as well as {@link ResourceLoaderAware},
* {@link ApplicationEventPublisherAware} and {@link MessageSourceAware} beans.
*
* 翻译:
* <p>除了标准的{@link org.springframework.beans.factory.BeanFactory}
* 生命周期能力,ApplicationContext 实现检测和调用
* {@link ApplicationContextAware} bean 以及 {@link ResourceLoaderAware},
* {@link ApplicationEventPublisherAware} 和 {@link MessageSourceAware} bean。
*
* @author Rod Johnson
* @author Juergen Hoeller
* @see ConfigurableApplicationContext
* @see org.springframework.beans.factory.BeanFactory
* @see org.springframework.core.io.ResourceLoader
*/
public interface ApplicationContext extends ListableBeanFactory, HierarchicalBeanFactory,
MessageSource, ApplicationEventPublisher, ResourcePatternResolver {
2,拿到对应bean对象:
3,Spring怎么知道要创建什么bean?
这个就要根据我们写的注解或者XML配置了。
声明的是某一个具体bean对象的信息,bean的定义信息
代码注解:
@Controller
@Service
@Component
XML文件:
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="${jdbc.username}" />
<property name="url" value="${jdbc.url}" />
</bean>
这些信息经过BD对象(BeanDefinition 实际上是个接口)会被解析对象,存放到容器之中。
4,BD(BeanDefinition)对象:
/**
* A BeanDefinition describes a bean instance, which has property values,
* constructor argument values, and further information supplied by
* concrete implementations.
* 翻译:BeanDefinition 描述了一个Bean实例的属性值,构造函数参数值,进一步的信息提供的具体实现。
*
* <p>This is just a minimal interface: The main intention is to allow a
* {@link BeanFactoryPostProcessor} such as {@link PropertyPlaceholderConfigurer}
* to introspect and modify property values and other bean metadata.
*
* 翻译:这是一个最小的接口:主要目的是允许一个
* {@link BeanFactoryPostProcessor}{@link PropertyPlaceholderConfigurer}等
* 反思和修改属性值和其他Bean的元数据。
*
* @author Juergen Hoeller
* @author Rob Harrop
* @since 19.03.2004
* @see ConfigurableListableBeanFactory#getBeanDefinition
* @see org.springframework.beans.factory.support.RootBeanDefinition
* @see org.springframework.beans.factory.support.ChildBeanDefinition
*/
public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {