Ⅰ 新手弱弱的问,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...");
}
}