Spring 3.1没有xml,只是配置不起作用

时间:2011-03-20 23:08:10

标签: java spring jetty spring-3

所以我正在尝试实现Spring的3.1博客文章中提到的关于From XML to @Configuration的内容,但它不希望按照假设工作。这是我正在使用的web.xml(这是唯一的xml),MvcFeatures和MvcBeans或多或少与刚刚添加了几个我的bean的博客相同。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <servlet>
        <servlet-name>dispatcher</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>
                com.example.config.MvcFeatures 
                com.example.config.MvcBeans
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file></welcome-file>
    </welcome-file-list>

</web-app>

尝试启动此功能时,我会在控制台中收到这些消息:

21 Mar 2011 00:52:58,203 INFO  org.springframework.web.context.support.AnnotationConfigWebApplicationContext []: No annotated classes found for specified class/package [com.example.config.MvcFeatures]
21 Mar 2011 00:52:58,203 INFO  org.springframework.web.context.support.AnnotationConfigWebApplicationContext []: No annotated classes found for specified class/package [com.example.config.MvcBeans]

任何想法都可能出错?据我所知,我认为它不喜欢contextConfigLocation参数值。

编辑:添加MvcFeatures以防万一..

@FeatureConfiguration
public class MvcFeatures {

    /**
     * Enables the Spring MVC @Controller programming model.
     */
    @Feature
    public MvcAnnotationDriven annotationDriven(ConversionService conversionService) {
        return new MvcAnnotationDriven().conversionService(conversionService)
                .argumentResolvers(new CustomArgumentResolver());
    }

    /**
     * Maps '/' requests to the 'home' view.
     */
    @Feature
    public MvcViewControllers viewController() {
        return new MvcViewControllers("/", "index");
    }

    /**
     * Enables Spring's component scanning feature.
     */
    @Feature
    public ComponentScanSpec componentScan() {
        return new ComponentScanSpec("com.example.controllers").excludeFilters(
                new AnnotationTypeFilter(Configuration.class), new AnnotationTypeFilter(
                        FeatureConfiguration.class));
    }
}

2 个答案:

答案 0 :(得分:2)

尝试

    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            com.example.config.**
        </param-value>
    </init-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

答案 1 :(得分:0)

理查兹,试试这些。

  1. contextConfigLocation = com.example.config
  2. 使用@Configuration和@EnableWebMvc
  3. 标记MvcFeatures类