Spring Data Rest:ResourceProcessor 配置无法正常工作 [英] Spring Data Rest: ResourceProcessor configuration is not working properly

查看:63
本文介绍了Spring Data Rest:ResourceProcessor 配置无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Spring Data Rest 实现(版本 2.5.2.RELEASE)有一个奇怪的行为.

I have a strange behaviour with a Spring Data Rest implementation (version 2.5.2.RELEASE).

我正在尝试注册 ResourceProcessor>@Bean,但有一些奇怪的东西.

I'm trying to register a @Bean of ResourceProcessor<Resource<Entity>>, but there is something strange.

我正在尝试两种解决方案:

I'm trying with two kinds of solutions:

1) 在类中声明@Bean:

@Bean
public ResourceProcessor<Resource<Author>> authorProcessor() {

    return new ResourceProcessor<Resource<Author>>() {

        @Override
        public Resource<Author> process(Resource<Author> resource) {

            System.out.println("method process of bean ResourceProcessor of class RepositoryBaseConfiguration");
            return resource;
        }
    };
}

2) 实现接口ResourceProcessor:

@Component
public class AuthorResourceProcessor implements ResourceProcessor<Resource<Author>> {

    @Override
    public Resource<Author> process(Resource<Author> resource) {
        System.out.println("method process of class AuthorResourceProcessor");
        return resource;
    }

}

完全忽略处理器:永远不会打印消息.

我注意到类 org.springframework.data.rest.webmvc.ResourceProcessorInvoker 有一个构造函数:

I noticed that the class org.springframework.data.rest.webmvc.ResourceProcessorInvoker has a constructor:

public ResourceProcessorInvoker(Collection<ResourceProcessor<?>> processors) {

    //...
}

这个构造函数在应用程序开始时被调用了 2 次,而不是只调用一次(正如我所期望的),我不明白为什么.

This constructor is invoked 2 times at the start of the application instead of only one time (as I will expect), and I don't understand why.

第一次,processors"变量用两个 bean(如预期的那样)和 bean org.springframework.data.rest.webmvc.ProfileResourceProcessor 求解.

The first time, the "processors" variable is solved with the two beans (as expected) and with the bean org.springframework.data.rest.webmvc.ProfileResourceProcessor.

但是第二次,processors"变量只用 bean org.springframework.data.rest.webmvc.ProfileResourceProcessor 解决.

But the second time, the "processors" variable is solved with only the bean org.springframework.data.rest.webmvc.ProfileResourceProcessor.

第二个配置@Override 第一个.

The second configuration @Override the first one.

有什么想法吗?

推荐答案

问题取决于应用程序启动时加载的配置.

The problem depends on the configurations loaded at the startup of the application.

我在 web.xml 上有这个配置:

I had this configuration on the web.xml:

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

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/spring-web-config.xml</param-value>
</context-param>

<servlet>
    <servlet-name>rest</servlet-name>
    <servlet-class>org.springframework.data.rest.webmvc.RepositoryRestDispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

所以,ContextLoaderListener第一次加载了正确的配置;servletRepositoryRestDispatcherServlet"的load-on-startup"属性启动第二个上下文配置加载.

So, the ContextLoaderListener loaded the correct configuration in the first time; the "load-on-startup" property of the servlet "RepositoryRestDispatcherServlet" launch a second context configuration load.

我也有一个扩展org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration的自定义类,但是这个自定义类在RepositoryRestDispatcherServlet<的构造函数时被忽略了/code> 加载默认的 RepositoryRestMvcConfiguration导致配置丢失.

I also had a custom class that extended org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration, but this custom class was ignored by the moment that the constructor of RepositoryRestDispatcherServlet load the default RepositoryRestMvcConfiguration, causing the lost of the configurations.

为了解决这个问题,我以这种方式创建了一个 custom RepositoryRestDispatcherServlet:

To solve that issue I have created a custom RepositoryRestDispatcherServlet in this way:

public class AppRepositoryRestDispatcherServlet extends DispatcherServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public AppRepositoryRestDispatcherServlet() {
        configure();
    }

    public AppRepositoryRestDispatcherServlet(WebApplicationContext webApplicationContext) {
        super(webApplicationContext);
        configure();
    }

    private void configure() {
        setContextClass(AnnotationConfigWebApplicationContext.class);
        setContextConfigLocation(RepositoryBaseConfiguration.class.getName());
    }

}

该类与RepositoryRestDispatcherServlet 相同,唯一不同的是在setContextConfigLocation 中传递了扩展RepositoryRestMvcConfiguration 的自定义类(RepositoryBaseConfiguration在这个例子中).

The class is the same as RepositoryRestDispatcherServlet, with the only difference that in the setContextConfigLocation is passed the custom class that extends RepositoryRestMvcConfiguration (RepositoryBaseConfiguration in this example).

显然我必须更新 web.xml 如下:

Obviously I had to update the web.xml as follows:

<servlet>
    <servlet-name>rest</servlet-name>
    <servlet-class>my.package.AppRepositoryRestDispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

这样,配置就被正确加载和维护了.

In this way, the configuration is correctly loaded and mantained.

这篇关于Spring Data Rest:ResourceProcessor 配置无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆