国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Spring框架初始化解析

Spring框架初始化解析

2021-02-21 11:56江曉曼軟件園 Java教程

這篇文章主要介紹了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的方法

?
1
2
3
4
5
6
7
/**
   * Initialize the root web application context.
   */
  @Override
  public void contextInitialized(ServletContextEvent event) {
    initWebApplicationContext(event.getServletContext());
  }

進入initWebApplicationContext 方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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類,

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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掃描的包

?
1
2
3
4
5
<!-- 注冊@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初始化完成后調用邏輯代碼,其他的容器的初始化完成,則不做任何處理,修改后代碼

如下:

?
1
2
3
4
5
6
@Override
   public void onApplicationEvent(ContextRefreshedEvent event) {
    if(event.getApplicationContext().getParent() == null){//root application context 沒有parent,他就是老大.
       //需要執行的邏輯代碼,當spring容器初始化完成后就會執行該方法。
    }
   }

初始化的順序是:

Constructor > @PostConstruct > InitializingBean > init-method

總結

以上就是本文關于Spring框架初始化解析的全部內容,希望對大家有所幫助。如有問題可以隨時留言,小編會及時回復大家的。感謝朋友們對本站的支持!

原文鏈接:http://blog.csdn.net/simplemurrina/article/details/70991008

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成人毛片在线 | 久久精国产 | 久久国产综合 | av网站一区 | 四虎免费紧急入口观看 | 精品久久久av | 欧美精品福利 | 香蕉成人啪国产精品视频综合网 | 成人福利电影 | 国产不卡免费视频 | 亚洲精品视| 国产目拍亚洲精品99久久精品 | 午夜色播 | 成人精品在线观看 | 国产精品女同一区二区免费站 | 亚洲香蕉视频 | 久久综合中文字幕 | 久久草在线视频 | 久久久精品天堂 | 网站av | 亚洲黄色免费 | 亚洲欧美激情精品一区二区 | 亚洲一区二区在线播放 | 精品一区免费 | 久久久高清 | 日本久久精品 | 欧洲另类二三四区 | 日韩欧美中文在线观看 | 日韩中文一区二区三区 | 精品国产欧美一区二区 | 国产亚洲精品精品国产亚洲综合 | 亚洲精品久久久一区二区三区 | 色爱欧美 | 日本久久精品视频 | 亚洲天堂一区二区 | 中文字幕av亚洲精品一部二部 | 欧美日韩日本国产 | 久久久精品一区二区 | 精品国产91乱码一区二区三区 | 国产精品99精品久久免费 | 女人高潮视频网站 |