我在Web应用程序中一起使用jsf和spring。我已经在一个配置类中配置了数据源和会话工厂,该配置类使用了诸如此类的注释@Configuration, @ComponentScan。我在我的项目中没有任何applicationContext.xml文件,因为我正在处理Configuration类中上下文xml的每个条目。该测试用例成功运行,但是当我部署Web应用程序时,它给了我错误
@Configuration, @ComponentScan
java.lang.IllegalStateException:找不到WebApplicationContext:没有注册ContextLoaderListener吗?
现在,如果我在web.xml中提供侦听器类,
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
它给我错误,
找不到/WEB-INF/applicationContext.xml
根据的文档ContextLoaderListener,的确是,如果我没有明确给出contextConfigLocationparam web.xml,它将搜索名为applicationContext.xml中的默认spring上下文文件web.xml。现在,如果我不想使用spring上下文文件,并使用注释进行所有配置,该怎么办?我应该如何注册侦听器类,ContextLoaderListener以便在不使用xml文件且仅使用批注的情况下,能够使用spring和jsf运行Web应用程序?
ContextLoaderListener
contextConfigLocationparam web.xml
applicationContext.xml
在web.xml你需要引导上下文AnnotationConfigWebApplicationContext:
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>
并且不要忘了使用@EnableWebMvcMVC注释。
@EnableWebMvcMVC
EDIT as a “comments follow up” => to be Turing Complete:
是的,你当然需要听众。尽管以上内容完全回答了“ 如何在Web.xml中注册Spring @Configuration带注释的类而不是applicationContext.xml文件 ”的问题,但这是Spring官方文档中的一个示例,其布局完整web.xml:
@Configuration
<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>