温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring框架初始化的示例分析

发布时间:2021-08-13 08:52:58 来源:亿速云 阅读:94 作者:小新 栏目:编程语言

这篇文章将为大家详细讲解有关Spring框架初始化的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

一、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框架初始化的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI