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

发布于 2024-10-24 18:46:11 字数 3251 浏览 3 评论 0原文

所以我试图实现 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));
    }
}

So I'm trying to implement things mentioned in Spring's 3.1 blog post about From XML to @Configuration, but it doesn't want to work as supposed. Here is the web.xml (and that's the only xml) I'm using and the MvcFeatures and MvcBeans more or less are the same as in the blog just added few my beans.

<?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>

When trying to launch this thing up I get these messages in console:

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]

Any ideas what could be wrong? From what I understand i think it doesn't like the contextConfigLocation param values.

EDIT: Adding the MvcFeatures in case it helps..

@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));
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

锦爱 2024-10-31 18:46:11

尝试

    <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

Try

    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            com.example.config.**
        </param-value>
    </init-param>

Or set the delimiters accordantly to the docs:

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
妄断弥空 2024-10-31 18:46:11

理查兹,试试这些。

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

Richards, try these.

  1. contextConfigLocation = com.example.config
  2. mark MvcFeatures class with @Configuration and @EnableWebMvc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文