如何在web.xml中注册Spring @Configuration注释类而不是applicationContext.xml文件?

我在web应用程序中使用jsf和spring。 我已经configuration了数据源和会话工厂在一个configuration类,它使用@Configuration, @ComponentScan Configuration @Configuration, @ComponentScan等注释。 我没有任何applicationContext.xml文件在我的项目中,因为我正在处理Configuration类中的上下文xml的每个条目。 testing用例能够成功运行,但是当我部署我的web应用程序时,它给了我错误

java.lang.IllegalStateException:没有findWebApplicationContext:没有ContextLoaderListener注册?

现在,如果我在web.xml中给监听器类,

 <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> 

它给我错误,

/WEB-INF/applicationContext.xml找不到

根据ContextLoaderListener的文档,如果我没有明确地给web.xml contextConfigLocation参数,它会在web.xmlsearch名为applicationContext.xml的默认spring上下文文件。 现在,如果我不想使用spring上下文文件并执行所有带注释的configuration,该怎么办? 我应该如何注册监听器类ContextLoaderListener以便不使用XML文件,只使用注释,我可以运行我的Web应用程序与spring和JSF?

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注解join。

进一步阅读:

  • Spring 3.1 MVC增强
  • Spring 3.1 MVC命名空间增强和configuration

编辑作为“意见后续”=>是图灵完成:

当然,你需要一个听众。 虽然上述内容完全回答了“ 如何注册Spring @Configuration注释类而不是web.xml中的applicationContext.xml文件 ”这个问题,下面是一个来自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> 

在这里碰到一个老问题,但是最近的Spring(v3.0 +)版本现在可以完全摆脱web.xml,只要您在支持Servlet 3.0+的Web容器上部署应用程序。

我们可以实现Spring的WebApplicationInitializer接口来执行与web.xml中相同的configuration。 这个实现类将被运行在Servlet 3.0+容器上的Spring 3.0+应用程序自动检测到。

如果设置很简单,你可以使用Spring提供的另一个类,如下所示。 这里所做的一切就是设置@Configuration类并列出servlet映射。 保持设置非常简单。

 public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer{ @Override protected Class<?>[] getRootConfigClasses() { return null; } @Override protected Class<?>[] getServletConfigClasses() { return new Class[] {AppConfig.class}; } @Override protected String[] getServletMappings() { return new String[] { "*.html" ,"*.json" ,"*.do"}; } }