當前位置:首頁 » 網頁前端 » spring自帶web容器
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

spring自帶web容器

發布時間: 2022-05-01 10:46:18

Ⅰ 新手弱弱的問,Spring是不是必須要在web容器中才能跑

不是,單機應用也可以使用spring

Ⅱ spring框架通過web容器來啟動的類

如下即為啟動spring的測試類,初始化spring上下文環境即可啟動spring,web項目只是把初始化spring上下文環境轉移到了web.xml里。
Action:

import org.apache.log4j.Logger;import org.springframework.context.support.;public class Action { private static Logger logger = Logger.getLogger(DisposeAction.class); private static String[] contexts = { "applicationContext.xml" }; public static void main(String[] args) { logger.info("[i] : preDispose Server start ... "); ctx = new (contexts); //SpringBeanManager.initContext(ctx); logger.info("[i] : preDispose Server start ok ... "); }}

Ⅲ 在基於spring搭建的java web應用中,是通過什麼方式觸發spring的初始化過程的

前段時間在公司做了一個項目,項目用了spring框架實現,WEB容器是Tomct 5,雖然說把項目做完了,但是一直對spring的IoC容器在web容器如何啟動和起作用的並不清楚。所以就抽時間看一下spring的源代碼,藉此了解它的原理。
我們知道,對於使用Spring的web應用,無須手動創建Spring容器,而是通過配置文件,聲明式的創建Spring容器。因此在Web應用中創建Spring容器有如下兩種方式:
1. 直接在web.xml文件中配置創建Spring容器。
2. 利用第三方MVC框架的擴展點,創建Spring容器。
其實第一種方式是更加常見。為了讓Spring容器隨Web應用的啟動而啟動,有如下兩種方式:
1. 利用ServletContextListener實現。
2. 利用load-on-startup Servlet實現。
Spring提供ServletContextListener的一個實現類ContextLoaderListener,該類可以作為Listener 使用,它會在創建時自動查找WEB-INF下的applicationContext.xml文件,因此,如果只有一個配置文件,並且文件名為applicationContext.xml,則只需在web.xml文件中增加以下配置片段就可以了。

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
如果有多個配置文件需要載入,則考慮使用<context-param...>元素來確定配置文件的文件名。ContextLoaderListener載入時,會查找名為contentConfigLocation的初始化參數。因此,配置<context-param...>時就指定參數名為contextConfigLocation。
帶多個配置文件的web.xml文件如下:

<context-param>
<param-name>contextLoaderListener</param-name>
<param-value>
WEB-INF/*.xml, classpath:spring/*.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
多個配置文件之間用「,」隔開。

下面我們來看它的具體實現過程是怎樣的,首先我們從ContextLoaderListener入手,它的代碼如下:

public class ContextLoaderListener implements ServletContextListener
{

private ContextLoader contextLoader;

/**
* 這個方法就是用來初始化web application context的
*/
public void contextInitialized(ServletContextEvent event)
{
this.contextLoader = createContextLoader();
this.contextLoader.initWebApplicationContext(event.getServletContext());
}

/**
* 創建一個contextLoader.
* @return the new ContextLoader
*/
protected ContextLoader createContextLoader()
{
return new ContextLoader();
}
................

}

Ⅳ spring是不是必須在web容器中才能跑

Web應用當然要在Web容器中應用啦,呵呵,但是測試什麼的,有框架可以模擬的,其他的都可以讀出applicationContext.xml的配置來應用的啊

Ⅳ 為什麼spring容器默認載入web-inf下的applicationContext.xml

就是spring代碼裡面,做了判斷,如果沒有指定配置文件,就會去WEB-INF中找。如果找不著,最後就報錯

Ⅵ java編程spring里org.springframework.web這個包是幹啥的

首先這個包是spring用來支持web應用的。
關於你說的監聽,其實是當web應用啟動時來執行的,舉個例子
org.springframework.web.context.ContextLoaderListener
比如這個類,我們會在web.xml中這樣來配置
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
這個監聽類的作用就是當spring發現<context-param>這個標簽內包含其他的xml配置文件,將這些xml文件對應的application content載入到web容器中,使得將多個application content建立起關聯。

希望對你有幫助,謝謝。

你說的很多種監聽,你現在沒必要花時間在這些細枝末節上,技術是為了應用服務的,根據需求去學習使用研究,會更好的。

Ⅶ java編程 spring 只能在啟動web容器的時候啟動么

如下即為啟動spring的測試類,初始化spring上下文環境即可啟動spring,web項目只是把初始化spring上下文環境轉移到了web.xml里。

Action:

importorg.apache.log4j.Logger;
importorg.springframework.context.support.;
publicclassAction{
privatestaticLoggerlogger=Logger.getLogger(DisposeAction.class);
privatestaticString[]contexts={
"applicationContext.xml"
};

publicstaticvoidmain(String[]args){
logger.info("[i]:preDisposeServerstart...");
ctx=new(contexts);
//SpringBeanManager.initContext(ctx);
logger.info("[i]:preDisposeServerstartok...");

}
}