我在 Web 应用程序中一起使用 jsf 和 spring。我在一个配置类中配置了数据源和会话工厂,该类使用注释,例如@Configuration, @ComponentScan
etc. 我的项目中没有任何 applicationContext.xml 文件因为我正在处理配置类中上下文 xml 的每个条目。测试用例成功运行,但是当我部署 Web 应用程序时,出现错误
java.lang.IllegalStateException:未找到 WebApplicationContext:否
ContextLoaderListener 已注册?
现在,如果我在 web.xml 中提供侦听器类,
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
它给了我错误,
找不到/WEB-INF/applicationContext.xml
根据文件ContextLoaderListener
,确实,如果我不给contextConfigLocation
参数输入web.xml
明确地,它将搜索名为的默认 spring 上下文文件applicationContext.xml
in web.xml
。现在,如果我不想使用 spring 上下文文件并使用注释进行所有配置,我该怎么办?我应该如何注册监听类ContextLoaderListener
这样,在不使用 xml 文件并仅使用注释的情况下,我就能够使用 spring 和 jsf 运行我的 Web 应用程序?
In web.xml
你需要引导上下文AnnotationConfigWebApplicationContext
:
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
org.package.YouConfigurationAnnotatedClass
</param-value>
</init-param>
</servlet>
并且不要忘记使用@EnableWebMvc
让您的 MVC 注释生效。
进一步阅读:
- Spring 3.1 MVC 增强 http://blog.springsource.com/2011/06/13/spring-3-1-m2-spring-mvc-enhancements-2/
- Spring 3.1 MVC命名空间增强和配置 http://blog.springsource.com/2011/02/21/spring-3-1-m1-mvc-namespace-enhancements-and-configuration/
编辑为“评论跟进” => 成为图灵完备:
是的,你当然需要一个倾听者。虽然上面完全回答了问题“如何在web.xml中注册Spring @Configuration注解的类而不是applicationContext.xml文件”,这里有一个example http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#beans-java-instantiating-container-web来自 Spring 官方文档,布局了完整的web.xml
:
<web-app>
<!-- Configure ContextLoaderListener to use AnnotationConfigWebApplicationContext
instead of the default XmlWebApplicationContext -->
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<!-- Configuration locations must consist of one or more comma- or space-delimited
fully-qualified @Configuration classes. Fully-qualified packages may also be
specified for component-scanning -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.acme.AppConfig</param-value>
</context-param>
<!-- Bootstrap the root application context as usual using ContextLoaderListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Declare a Spring MVC DispatcherServlet as usual -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- Configure DispatcherServlet to use AnnotationConfigWebApplicationContext
instead of the default XmlWebApplicationContext -->
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<!-- Again, config locations must consist of one or more comma- or space-delimited
and fully-qualified @Configuration classes -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.acme.web.MvcConfig</param-value>
</init-param>
</servlet>
<!-- map all requests for /app/* to the dispatcher servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
</web-app>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)