These essential spring of knowledge, you know how much? Come test

Spring principle
it is a comprehensive, one-stop enterprise application development solutions through the presentation layer, the business layer, persistence layer. But Spring still can seamlessly integrate with other frameworks

Spring features
lightweight
Inversion of Control
for cut
container
frame collection

These essential spring of knowledge, you know how much?  Come test
Spring core components

These essential spring of knowledge, you know how much?  Come test
Spring common module
These essential spring of knowledge, you know how much?  Come test
Spring main packet
These essential spring of knowledge, you know how much?  Come test
Spring annotation common
bean and assembly of the injection There are many ways, by xml, get set mode, or annotations and constructors. Simple and easy way is to use the Spring annotations, Spring provides a number of annotations way.

These essential spring of knowledge, you know how much?  Come test
Spring combined with third party

These essential spring of knowledge, you know how much?  Come test
Spring IOC principle
concept

Spring describes dependencies between Bean and Bean via a configuration file, using the Java language reflection function Bean instantiation and establishing dependencies between Bean. Spring IoC container Upon completion of these low-level work, but also provides a cache Bean instance, life cycle management, Bean instance agency, event publishing, resource loading and other advanced services.

Spring container top view

When reading the application provides Spring Bean boot configuration information and generate a corresponding Bean Spring container disposed in registry, and then according to this example of the registry Bean, assembled dependencies between Bean, for the upper application provide ready operating environment. Bean cache pool for which the HashMap implementation.
These essential spring of knowledge, you know how much?  Come test
IOC container to achieve
BeanFactory- framework for infrastructure

BeanFactory 是 Spring 框架的基础设施,面向 Spring 本身;ApplicationContext 面向使用Spring 框架的开发者,几乎所有的应用场合我们都直接使用 ApplicationContext 而非底层的 BeanFactory。

These essential spring of knowledge, you know how much?  Come test
1.1..1.1.1 BeanDefinitionRegistry 注册表

Spring 配置文件中每一个节点元素在 Spring 容器里都通过一个 BeanDefinition 对象表示,它描述了 Bean 的配置信息。而 BeanDefinitionRegistry 接口提供了向容器手工注册BeanDefinition 对象的方法。

1.1..1.1.2 BeanFactory 顶层接口

位于类结构树的顶端 ,它最主要的方法就是 getBean(String beanName),该方法从容器中返回特定名称的 Bean,BeanFactory 的功能通过其他的接口得到不断扩展:

1.1..1.1.3 ListableBeanFactory

该接口定义了访问容器中 Bean 基本信息的若干方法,如查看 Bean 的个数、获取某一类型Bean 的配置名、查看容器中是否包括某一 Bean 等方法;

1.1..1.1.4 HierarchicalBeanFactory 父子级联

父子级联 IoC 容器的接口,子容器可以通过接口方法访问父容器; 通过

HierarchicalBeanFactory 接口, Spring 的 IoC 容器可以建立父子层级关联的容器体系,子容器可以访问父容器中的 Bean,但父容器不能访问子容器的 Bean。Spring 使用父子容器实现了很多功能,比如在 Spring MVC 中,展现层 Bean 位于一个子容器中,而业务层和持久层的 Bean 位于父容器中。这样,展现层 Bean 就可以引用业务层和持久层的 Bean,而业务层和持久层的 Bean 则看不到展现层的 Bean。

1.1..1.1.5 ConfigurableBeanFactory

是一个重要的接口,增强了 IoC 容器的可定制性,它定义了设置类装载器、属性编辑器、容器初始化后置处理器等方法;

1.1..1.1.6 AutowireCapableBeanFactory 自动装配

定义了将容器中的 Bean 按某种规则(如按名字匹配、按类型匹配等)进行自动装配的方法;

1.1..1.1.7 SingletonBeanRegistry 运行期间注册单例 Bean

定义了允许在运行期间向容器注册单实例 Bean 的方法;对于单实例( singleton)的 Bean来说,BeanFactory 会缓存 Bean 实例,所以第二次使用 getBean() 获取 Bean 时将直接从IoC 容器的缓存中获取 Bean 实例。Spring 在DefaultSingletonBeanRegistry 类中提供了一个用于缓存单实例 Bean 的缓存器,它是一个用 HashMap 实现的缓存器,单实例的 Bean 以beanName 为键保存在这个 HashMap 中。

1.1..1.1.8 依赖日志框框

在初始化 BeanFactory 时,必须为其提供一种日志框架,比如使用 Log4J, 即在类路径下提供 Log4J 配置文件,这样启动 Spring 容器才不会报错。

ApplicationContext 面向开发应用

ApplicationContext 由 BeanFactory 派 生 而 来 , 提 供 了 更 多 面 向 实 际 应 用 的 功 能 。ApplicationContext 继承了 HierarchicalBeanFactory 和 ListableBeanFactory 接口,在此基础上,还通过多个其他的接口扩展了 BeanFactory 的功能:

These essential spring of knowledge, you know how much?  Come test
1、 ClassPathXmlApplicationContext:默认从类路径加载配置文件

2、 FileSystemXmlApplicationContext:默认从文件系统中装载配置文件

3、 ApplicationEventPublisher:让容器拥有发布应用上下文事件的功能,包括容器启动事件、关闭事件等。

4、 MessageSource:为应用提供 i18n 国际化消息访问的功能;

5、 ResourcePatternResolver : 所 有 ApplicationContext 实现类都实现了类似于

PathMatchingResourcePatternResolver 的功能,可以通过带前缀的 Ant 风格的资源文件路径装载 Spring 的配置文件。

6、 LifeCycle:该接口是 Spring 2.0 加入的,该接口提供了 start()和 stop()两个方法,主要用于控制异步处理过程。在具体使用时,该接口同时被 ApplicationContext 实现及具体Bean 实现, ApplicationContext 会将 start/stop 的信息传递给容器中所有实现了该接口的 Bean,以达到管理和控制 JMX、任务调度等目的。

7、 ConfigurableApplicationContext 扩展于 ApplicationContext,它新增加了两个主要的方法: refresh()和 close(),让 ApplicationContext 具有启动、刷新和关闭应用上下文的能力。在应用上下文关闭的情况下调用 refresh()即可启动应用上下文,在已经启动的状态下,调用 refresh()则清除缓存并重新装载配置信息,而调用 close()则可关闭应用上下文。

8、 WebApplication 体系架构
WebApplicationContext 是专门为 Web 应用准备的,它允许从相对于 Web 根目录的路径中装载配置文件完成初始化工作。从 WebApplicationContext 中可以获得ServletContext 的引用,整个 Web 应用上下文对象将作为属性放置到 ServletContext中,以便 Web 应用环境可以访问 Spring 应用上下文。

These essential spring of knowledge, you know how much?  Come test
Spring Bean 作用域
Spring 3 中为 Bean 定义了 5 中作用域,分别为 singleton(单例)、prototype(原型)、request、session 和 global session,5 种作用域说明如下:

singleton:单例模式(多线程下不安全)

1、 singleton:单例模式,Spring IoC 容器中只会存在一个共享的 Bean 实例,无论有多少个Bean 引用它,始终指向同一对象。该模式在多线程下是不安全的。Singleton 作用域是Spring 中的缺省作用域,也可以显示的将 Bean 定义为 singleton 模式,配置为:

<bean id="userDao" class="com.ioc.UserDaoImpl" scope="singleton"/>

prototype:原型模式每次使用时创建

2、 prototype:原型模式,每次通过 Spring 容器获取 prototype 定义的 bean 时,容器都将创建一个新的 Bean 实例,每个 Bean 实例都有自己的属性和状态,而 singleton 全局只有一个对象。根据经验,对有状态的bean使用prototype作用域,而对无状态的bean使用singleton作用域。

Request:一次 request 一个实例

3、 request:在一次 Http 请求中,容器会返回该 Bean 的同一实例。而对不同的 Http 请求则会产生新的 Bean,而且该 bean 仅在当前 Http Request 内有效,当前 Http 请求结束,该 bean实例也将会被销毁。

<bean id="loginAction" class="com.cnblogs.Login" scope="request"/>

session

4、 session:在一次 Http Session 中,容器会返回该 Bean 的同一实例。而对不同的 Session 请求则会创建新的实例,该 bean 实例仅在当前 Session 内有效。同 Http 请求相同,每一次session 请求创建新的实例,而不同的实例之间不共享属性,且实例仅在自己的 session 请求内有效,请求结束,则实例将被销毁。

<bean id="userPreference" class="com.ioc.UserPreference" scope="session"/>

global Session

5、 global Session:在一个全局的 Http Session 中,容器会返回该 Bean 的同一个实例,仅在使用 portlet context 时有效。

Spring Bean 生命周期
实例化

1、实例化一个 Bean,也就是我们常说的 new。

IOC 依赖注入

2、 按照 Spring 上下文对实例化的 Bean 进行配置,也就是 IOC 注入。

setBeanName 实现

3、 如果这个 Bean 已经实现了 BeanNameAware 接口,会调用它实现的 setBeanName(String)方法,此处传递的就是 Spring 配置文件中 Bean 的 id 值

BeanFactoryAware 实现

4、 如果这个 Bean 已经实现了 BeanFactoryAware 接口,会调用它实现的 setBeanFactory,setBeanFactory(BeanFactory)传递的是 Spring 工厂自身(可以用这个方式来获取其它 Bean,只需在 Spring 配置文件中配置一个普通的 Bean 就可以)。ApplicationContextAware 实现

5、如果这个 Bean 已经实现了 ApplicationContextAware 接口,会调用

setApplicationContext(ApplicationContext)方法,传入 Spring 上下文(同样这个方式也可以实现步骤 4 的内容,但比 4 更好,因为 ApplicationContext 是 BeanFactory 的子接口,有更多的实现方法)postProcessBeforeInitialization 接口实现-初始化预处理

6、如果这个 Bean 关联了 BeanPostProcessor 接口,将会调用
postProcessBeforeInitialization(Object obj, String s)方法,BeanPostProcessor 经常被用作是 Bean 内容的更改,并且由于这个是在 Bean 初始化结束时调用那个的方法,也可以被应用于内存或缓存技术。

init-method

7、如果 Bean 在 Spring 配置文件中配置了 init-method 属性会自动调用其配置的初始化方法。

postProcessAfterInitialization

8, if the Bean associated with the BeanPostProcessor interface will be called

postProcessAfterInitialization(Object obj, String s)方法。

Note: After completion of the above can be applied to this Bean, that the Bean is a Singleton, so under normal circumstances we call the same id of Bean will be the same instance in the content address, of course, can also be configured in the Spring configuration file non-Singleton.

Destroy expired automatic cleanup stage

1, when Bean is no longer needed, it will clean-up stage, if the Bean implements this interface DisposableBean, calls the destroy () method of its implementation; destroy-method self-configuring cleanup

2. Finally, if the Bean in Spring configuration to configure the destroy-method attributes, its configuration will automatically destroy method is called.

These essential spring of knowledge, you know how much?  Come test
3, bean tag has two important properties (init-method and destroy-method). You can use them to customize their own initialization and logout methods. They also have corresponding comments (@PostConstruct and @PreDestroy).

<Bean id = "" class = "" init-method = "Initialization Method" destroy-method = "Destruction Method">

To be continued ............

Need the full pdf document junior partner
please poke "" "" "" " https://shimo.im/docs/TC9Jq63Tp6HvTXdg/

If omissions, please correct me, thank you! !

Guess you like

Origin blog.51cto.com/14587687/2463940