简体   繁体   English

Spring - 注入验证器bean的麻烦

[英]Spring - trouble with injecting validator bean

i couldn't find solution for my problem anywhere. 我找不到任何问题的解决方案。 I'm trying to make validation working in spring web flow form. 我正在尝试使用spring web流式表单进行验证。 I need to set validator in configuration, however it's located in another config file and it seems spring can't find proper bean. 我需要在配置中设置验证器,但它位于另一个配置文件中,似乎spring无法找到合适的bean。 How can I achieve successful injection here? 我怎样才能在这里成功注射? As far as I know, Autowiring should inject bean into validator reference. 据我所知,自动装配应该将bean注入验证器参考。 Maybe it has something to do with order of loading configuration classes? 也许它与加载配置类的顺序有关?

WebConfig.java: WebConfig.java:

@Configuration
@Import(godziszewski.patryk.ElectronicsStore.config.FlowConfiguration.class)
@EnableWebMvc
@ComponentScan(basePackages = "godziszewski.patryk")
public class WebConfig extends WebMvcConfigurerAdapter {
    ....
    @Bean
    public LocalValidatorFactoryBean validator()
    {
        LocalValidatorFactoryBean lv = new LocalValidatorFactoryBean();
        lv.setValidationMessageSource(messageSource());
        return lv;
    }
}

FlowConfiguration.java: FlowConfiguration.java:

@Configuration
public class FlowConfiguration extends AbstractFlowConfiguration {
    @Autowired
    Validator validator;
    ....

    @Bean
    public FlowBuilderServices flowBuilderServices()
    {
        FlowBuilderServices fbs = new FlowBuilderServices();
        fbs.setValidator(validator);
        return fbs;
    }
}

The error i'm getting: 我得到的错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: 
  Error creating bean with name 'flowConfiguration': 
  Unsatisfied dependency expressed through field 'validator': 
  No qualifying bean of type [org.springframework.validation.Validator] found for dependency [org.springframework.validation.Validator]: 
  expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)};
  nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
    No qualifying bean of type [org.springframework.validation.Validator] found for dependency [org.springframework.validation.Validator]: 
    expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Im using spring 4.3.2.RELEASE 我正在使用弹簧4.3.2.RELEASE

However, when I delete Validator dependency from FlowAdapter.class I get error : 但是,当我从FlowAdapter.class中删除Validator依赖项时,我收到错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flowBuilderServices' defined in class path resource [godziszewski/patryk/ElectronicsStore/config/FlowConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: The ViewFactoryCreator is required
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:381)
    at godziszewski.patryk.ElectronicsStore.config.FlowConfiguration$$EnhancerBySpringCGLIB$$b65e14d6.flowBuilderServices(<generated>)
    at godziszewski.patryk.ElectronicsStore.config.FlowConfiguration.flowRegistry(FlowConfiguration.java:25)
    at godziszewski.patryk.ElectronicsStore.config.FlowConfiguration$$EnhancerBySpringCGLIB$$b65e14d6.CGLIB$flowRegistry$3(<generated>)
    at godziszewski.patryk.ElectronicsStore.config.FlowConfiguration$$EnhancerBySpringCGLIB$$b65e14d6$$FastClassBySpringCGLIB$$e5741e7e.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356)
    at godziszewski.patryk.ElectronicsStore.config.FlowConfiguration$$EnhancerBySpringCGLIB$$b65e14d6.flowRegistry(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    ... 24 more
Caused by: java.lang.IllegalArgumentException: The ViewFactoryCreator is required

Full FlowConfiguration class code, maybe I'm doing something wrong? 完整的FlowConfiguration类代码,也许我做错了什么?

package godziszewski.patryk.ElectronicsStore.config;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.Validator;
import org.springframework.webflow.config.AbstractFlowConfiguration;
import org.springframework.webflow.definition.registry.FlowDefinitionRegistry;
import org.springframework.webflow.engine.builder.support.FlowBuilderServices;
import org.springframework.webflow.executor.FlowExecutor;
import org.springframework.webflow.mvc.servlet.FlowHandlerAdapter;
import org.springframework.webflow.mvc.servlet.FlowHandlerMapping;


@Configuration
public class FlowConfiguration extends AbstractFlowConfiguration {


    @Bean
    public FlowDefinitionRegistry flowRegistry() {
        return getFlowDefinitionRegistryBuilder()
            .setBasePath("/WEB-INF/flows")
            .setFlowBuilderServices(flowBuilderServices())
            .addFlowLocationPattern("/**/*-flow.xml")
            .build();
    }
    @Bean
    public FlowExecutor flowExecutor() {
        return getFlowExecutorBuilder(flowRegistry()).build();
    }

    @Bean
    public FlowHandlerMapping flowHandlerMapping()
    {
        System.out.println("flowconfig");
        FlowHandlerMapping fh = new FlowHandlerMapping();
        fh.setFlowRegistry(flowRegistry());
        return fh;
    }
    @Bean
    public FlowHandlerAdapter flowHandlerAdapter()
    {
        FlowHandlerAdapter fh = new FlowHandlerAdapter();
        fh.setFlowExecutor(flowExecutor());
        return fh;
    }
    @Bean
    public FlowBuilderServices flowBuilderServices()
    {
        FlowBuilderServices fbs = new FlowBuilderServices();
        //fbs.setValidator(validator);
        return fbs;
    }
}

If i delete .setFlowBuilderServices(flowBuilderServices()) method, everything works fine 如果我删除.setFlowBuilderServices(flowBuilderServices())方法,一切正常

EDIT: I managed to get this working by deleting @Configuration annotation form flow config class, now it looks like this: 编辑:我设法通过删除@Configuration注释表单流配置类来实现这一点,现在它看起来像这样:

//@Configuration
public class FlowConfiguration extends AbstractFlowConfiguration {

    @Autowired
    Validator validator;

    ...
    @Bean
    public FlowBuilderServices flowBuilderServices()
    {
        System.out.println(validator.toString());
        FlowBuilderServices fbs = new FlowBuilderServices();
        fbs.setValidator(validator);
        return fbs;
    }

And now I can use injected LocalValidatorBean in this class. 现在我可以在这个类中使用注入的LocalValidatorBean。

I interpret that eventually you need a FlowBuilderServices which has a reference to Validator bean. 我解释说最终你需要一个FlowBuilderServices,它引用了Validator bean。

This can be achieved by using the Autowired tag inside FlowBuilderServices 这可以通过使用FlowBuilderServices中的Autowired标记来实现

public class FlowBuilderServices{
@Autowired
Validator validator

...
}

And then in FlowConfiguration you just need to define the bean 然后在FlowConfiguration中你只需要定义bean

@Bean
public class FlowBuilderServices flowBuilderServices()
    {
        FlowBuilderServices fbs = new FlowBuilderServices();
        return fbs;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM