有沒有一種方法可以在Spring MVC應用程序中使用web.xml加載上下文?使用web.xml在Spring中加載上下文
回答
來自spring文檔
Spring可以很容易地集成到任何基於Java的web框架中。您只需要在您的web.xml中聲明ContextLoaderListener,並使用contextConfigLocation來設置要加載的上下文文件。
的<context-param>
:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
然後可以使用WebApplicationContext中得到您的豆柄。
WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servlet.getServletContext());
SomeBean someBean = (SomeBean) ctx.getBean("someBean");
還可以指定上下文位置相對電流的類路徑,其可以是優選的
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
'*'的意義是什麼?沒有它,它就無法工作:'IOException從ServletContext資源解析XML文檔[/>classpath:/applicationContext.xml];嵌套的異常是java.io.FileNotFoundException:無法打開ServletContext資源[/> classpath:/applicationContext.xml]' – DavidS
我剛剛發現了一篇博客文章,回答關於'classpath *'[這裏](http:// www。 gridshore.nl/2008/05/13/spring-application-context-loading-tricks/)。 – DavidS
也可以在定義的servlet本身加載的上下文( WebApplicationContext)
<servlet>
<servlet-name>admin</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/*.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>admin</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
而不是(的ApplicationContext)
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
或可以兩者都做在一起。只是使用的WebApplicationContext的
缺點是,它會加載上下文只爲與上述方法的上下文將被加載爲多個入口點這個特定彈簧的入口點(DispatcherServlet
)(例如。Webservice Servlet, REST servlet
等)
上下文通過ContextLoaderListener
加載將事實上是專門爲DisplacherServlet加載的父上下文。因此,基本上可以在應用程序上下文中加載所有業務服務,數據訪問或存儲庫bean,並分離出控制器,並將解析器bean查看到WebApplicationContext。
- 1. 在web.xml中加載上下文
- 2. 如何使用web.xml和Tomcat在Spring中正確加載上下文
- 3. web.xml加載類啓動到上下文
- 4. Spring上下文加載器
- 5. 在不使用web.xml的情況下加載應用程序上下文
- 6. 使用特定的類加載器加載Spring上下文
- 7. Spring 3應用上下文加載
- 8. 加載servlet-context(Spring MVC)時,應用上下文未加載
- 9. Application Server和Spring上下文加載
- 10. 加載部分Spring上下文
- 11. Spring + JAX.RS - 上下文加載兩次
- 12. Jrebel影響Spring上下文加載?
- 13. 如何在web.xml中添加應用程序上下文
- 14. 首先加載的Web.xml(tomcat)vs ApplicationContext.xml(spring)?
- 15. 在上下文文件中禁止加載Spring URL資源
- 16. Spring 3.1 contextInitializerClasses不使用web.xml工作在WebLogic 10.3.6上下文參數
- 17. 使用工具加載Spring集成測試上下文
- 18. 如何使用web.xml上下文參數在Seam應用程序中加載config.properties文件
- 19. Spring和Hibernate:將cfg.xml加載到應用程序上下文中
- 20. Spring @Value訪問屬性在父上下文中加載
- 21. 在.war文件中使用web.xml的Spring啓動應用程序
- 22. Spring應用程序上下文無法加載配置文件
- 23. 在Spring Rest Web Service上下載文件
- 24. Spring @DirtiesContext是否重新加載Spring上下文?
- 25. Spring應用程序上下文用Autowire手動加載
- 26. UIPageViewController - 使用epub上下文加載
- 27. 在一些servlet之後在web應用程序中加載spring上下文
- 28. 在運行時在Web應用程序中動態加載Spring上下文
- 29. 加載上下文
- 30. 在沒有web.xml的情況下使用spring 4.1初始化log4j
如何訪問上下文?並且你是否說應用程序一啓動,上下文就會加載Spring Context?請澄清一下,因爲我對Spring很陌生。感謝您的回覆 – tamilnad
查看更新回答 – ddewaele
以下是與WebApplicationContextUtils相關的最新API的鏈接。 https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/context/support/WebApplicationContextUtils.html – Ajitesh