Spring原理深入01:什么是Spring?Spring基本机制?IOC?AOP

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 {

5,实现一个XML到BD对象思路:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值