当前位置: 移动技术网 > IT编程>开发语言>Java > Spring框架初始化解析

Spring框架初始化解析

2019年07月19日  | 移动技术网IT编程  | 我要评论

一、spring能做什么?

spring的主要目的是使j2ee易用和促进好编程习惯。

倒置控制容器 spring的设计核心是 org.springframework.beans 包, 为与javabeans一起工作而设计。 这个包一般不直接被用户使用, 但作为基础为更多的其他功能服务. 下一个较高层面的抽象是"bean factory"。 spring bean factory 是一个普通的factory,它使对象能够按名称获取,并且能管理对象之间的关系。 bean factories 支持两种对象模式: . singleton:在此模式中,有一个具有特定名称的共享对象实例,它在查找时被获取。这是默认的,而且是最为经常使用的。它对于无状态对象是一种理想的模式。 .prototype:在此模式中,每次获取将创建一个独立的对象。

二、spring启动加载及实现方式

第一种:通过注解@postconstruct 和 @predestroy 方法 实现初始化和销毁bean之前进行的操作

第二种:通过 在xml中定义init-method 和 destory-method方法

第三种:通过bean实现initializingbean和 disposablebean接口

第四种:写一个类,实现beanpostprocessor接口,这个接口有两个方法。

(1):postprocessbeforeinitialization方法,在spring中定义的bean初始化前调用这个方法
(2):postprocessafterinitialization方法,在spring中定义的bean初始化后调用这个方法
或实现
instantiationawarebeanpostprocessor,是beanpostprocessor的子接口
spring 容器加载完成后执行

从spring监听器作为入口。

org.springframework.web.context.contextloaderlistener

找到初始化spring的方法

/** 
   * initialize the root web application context. 
   */ 
  @override 
  public void contextinitialized(servletcontextevent event) { 
    initwebapplicationcontext(event.getservletcontext()); 
  } 

进入initwebapplicationcontext 方法

if (this.context == null) { 
  this.context = createwebapplicationcontext(servletcontext); 
} 
if (this.context instanceof configurablewebapplicationcontext) { 
  configurablewebapplicationcontext cwac = (configurablewebapplicationcontext) this.context; 
  if (!cwac.isactive()) { 
    // the context has not yet been refreshed -> provide services such as 
    // setting the parent context, setting the application context id, etc 
    if (cwac.getparent() == null) { 
      // the context instance was injected without an explicit parent -> 
      // determine parent for root web application context, if any. 
      applicationcontext parent = loadparentcontext(servletcontext); 
      cwac.setparent(parent); 
    } 
    configureandrefreshwebapplicationcontext(cwac, servletcontext); 
  } 
} 

applicationlistener

1、编写一个实现applicationlistener的listener类,

import org.springframework.context.applicationlistener;
import org.springframework.context.event.contextrefreshedevent;
import org.springframework.stereotype.service;
@service
public class startuplistenerimplements
 applicationlistener<contextrefreshedevent>
{
  @override
  public void onapplicationevent(contextrefreshedevent event)
  {
    if(event.getapplicationcontext().getparent() == null)//root application context 没有parent,他就是老大.
    { 
      //需要执行的逻辑代码,当spring容器初始化完成后就会执行该方法。 
      system.out.println("\n\n\n\n\n______________\n\n\n加载了\n\n_________\n\n");
    } 
    
    //或者下面这种方式
    if(event.getapplicationcontext().getdisplayname().equals("root webapplicationcontext"))
    {
      system.out.println("\n\n\n_________\n\n加载一次的 \n\n ________\n\n\n\n");
    }
    
  }
}

2、在配置文件(applicationcontext-servlet.xml)中设置service扫描的包

<!-- 注册@controller 、@service-->
  <context:component-scan base-package="com.test.controller" use-default-filters="false">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.controller" />
    <context:include-filter type="annotation" expression="org.springframework.stereotype.service" />
  </context:component-scan>

3、部署启动项目,即可在加载完spring后就打印出“加载”

applicationontext和使用mvc之后的webapplicationontext会两次调用上面的方法,如何区分这个两种容器呢?

但是这个时候,会存在一个问题,在web项目中(springmvc),系统会存在两个容器,一个是rootapplicationcontext,另一个就是我们自己的projectname-servletcontext(作为rootapplicationcontext的子容器)。

这种情况下,就会造成onapplicationevent方法被执行两次。为了避免上面提到的问题,我们可以只在rootapplicationcontext初始化完成后调用逻辑代码,其他的容器的初始化完成,则不做任何处理,修改后代码

如下:

@override 
   public void onapplicationevent(contextrefreshedevent event) { 
    if(event.getapplicationcontext().getparent() == null){//root application context 没有parent,他就是老大. 
       //需要执行的逻辑代码,当spring容器初始化完成后就会执行该方法。 
    } 
   } 

初始化的顺序是:

constructor > @postconstruct > initializingbean > init-method

总结

以上就是本文关于spring框架初始化解析的全部内容,希望对大家有所帮助。如有问题可以随时留言,小编会及时回复大家的。感谢朋友们对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网