web项目启动Spring容器
2013-02-28 19:38
417 查看
今天在重新搭建一套web框架,只想采用spring,其它框架均用不到。使用java应用程序进行测试,完全没问题。考虑到做为web应用程序,那就必须要在服务器启动时,能够将spring的上下文配置加载进去,并启动Spring容器,如何做呢?一般在web项目中启动Spring容器,只需要在web.xml中新增
一个配置即可,具体的配置示例如下所示:
1. 首先,在web.xml中配置spring的配置文件的位置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/applicationContext.xml</param-value>
</context-param>
注:context-param中定义的是application范围内的参数,存放在servletcontext中
2. 需要在web.xml中定义listener
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
ContextLoaderListener实现ServletContextListener,读取contextConfigLocation中定义的xml文件,如果不设置contextConfigLocation的初始参数则默认会读取WEB-INF路径下的 application.xml文件。ContextLoaderListener读取这些XML文件并产生
WebApplicationContext对象,然后将这个对象放置在ServletContext的属性里,这样我们只要可以得到Servlet就可以得到WebApplicationContext对象,并利用这个对象访问spring容器管理的bean。 经过上述的配置,即可以在应用服务器启动时,将spring容器启动起来。
一个配置即可,具体的配置示例如下所示:
1. 首先,在web.xml中配置spring的配置文件的位置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/applicationContext.xml</param-value>
</context-param>
注:context-param中定义的是application范围内的参数,存放在servletcontext中
2. 需要在web.xml中定义listener
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
ContextLoaderListener实现ServletContextListener,读取contextConfigLocation中定义的xml文件,如果不设置contextConfigLocation的初始参数则默认会读取WEB-INF路径下的 application.xml文件。ContextLoaderListener读取这些XML文件并产生
WebApplicationContext对象,然后将这个对象放置在ServletContext的属性里,这样我们只要可以得到Servlet就可以得到WebApplicationContext对象,并利用这个对象访问spring容器管理的bean。 经过上述的配置,即可以在应用服务器启动时,将spring容器启动起来。
相关文章推荐
- web.xml中配置Spring容器随项目启动
- 在web项目中如何启动spring容器?
- web项目启动spring容器
- Java - 在Web项目中如何获得Spring的IoC容器?
- spring在web容器启动时执行初始化方法
- Spring在Web项目中的三种启动加载的配置
- 使用Spring Boot +JPA+mybatis快速启动一个web项目
- 【spring】IOC容器在WEB容器中的启动
- 【Spring容器】项目启动后初始化数据的两种实践方案
- Java - 如何在Web项目中配置Spring的IoC容器?
- maven项目启动报java.lang.ClassNotFoundException: org.springframework.web.util.IntrospectorCleanupListener
- Intellij IDEA 启动项目ClassNotFoundException:org.springframework.web.context.ContextLoaderListener
- Spring 学习-------WebApplication (web 容器)启动
- Spring mvc + mybatis web项目当系统启动时运行某方法查询数据库内容
- Spring深入挖掘:Spring在Web容器中是如何启动的
- maven的java web项目启动找不到Spring ContextLoaderListener的解决办法
- spring-boot项目按照平常的web项目一样发布到tomcat容器下
- spring+mybatis启动web项目报错:error create beans of ...Junit测试通过但是启动web项目报错
- S2SM项目tomcat启动停在Initializing Spring root WebApplicationContext
- Spring Cloud (8) | Spring Boot项目改造成tomcat容器启动