Spring webflow - 我必须手动设置应用程序上下文

时间:2016-09-10 09:21:18

标签: java spring spring-mvc spring-webflow

我的Spring应用程序与Spring Webflow有一个奇怪的问题。问题是,当我像文档中那样设置流程时:

//Instances
PersonDto dto = new PersonDto();
PersonDto.CarDto carDto = new PersonDto.CarDto();
carDto.setColor("blue");
carDto.setModel("Picasso");
dto.setCar(carDto);

PersonVo vo =  mapper.map(dto, PersonVo.class);

System.out.println(vo);

它在我的应用程序中找不到bean。我在这里描述了这个问题: Spring web flow can't resolve service bean? 但是当我像这样手动设置应用程序上下文时:

@Bean
public FlowBuilderServices flowBuilderServices()
{
    return getFlowBuilderServicesBuilder()
            .setValidator(validator)
            .build();
}

工作正常。我还没有看到有人像我一样手动设置应用程序配置,所以我认为我的应用程序的配置存在一些问题。 这是我的应用程序配置:

@Bean
public FlowBuilderServices flowBuilderServices()
{
    FlowBuilderServices fbs = getFlowBuilderServicesBuilder().build();
    fbs.setApplicationContext(getApplicationContext());
    fbs.setValidator(validator);
    return fbs;
}

Webconfig:

public class ElectronicsStoreWebInitialiser extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] { RootConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] { WebConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

    @Override
    protected void customizeRegistration(Dynamic registration) {
        registration.setMultipartConfig(
                new MultipartConfigElement("/tmp/images",
                        2097152, 4194304, 0));
    }
}

流量配置:

@Configuration
@Import(godziszewski.patryk.ElectronicsStore.config.FlowConfiguration.class)
@EnableWebMvc
@ComponentScan(basePackages = "godziszewski.patryk")
public class WebConfig extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true); 
        return resolver;
    }
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
         UrlPathHelper urlPathHelper = new UrlPathHelper();
         urlPathHelper.setRemoveSemicolonContent(false);
         configurer.setUrlPathHelper(urlPathHelper);
     }
    @Override
    public Validator getValidator() {
        return validator();
    }

    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resource/**").addResourceLocations("/resources/");
    }
    @Bean
    TilesConfigurer tilesConfigurer() {
        TilesConfigurer tiles = new TilesConfigurer();
        tiles.setDefinitions(new String[] {
                "/WEB-INF/tiles/definition/tile-definition.xml"
        });
        tiles.setCheckRefresh(true);
        return tiles;
    }

    @Bean
    public ViewResolver tilesViewResolver() {
        TilesViewResolver tv = new TilesViewResolver();
        tv.setOrder(-2);
        return  tv;
    }
    @Bean
    public ResourceBundleMessageSource messageSource() {
        ResourceBundleMessageSource rb = new ResourceBundleMessageSource();
        rb.setBasename("messages");
        return rb;
    }
    @Bean
    public StandardServletMultipartResolver multipartResolver() {
        return new StandardServletMultipartResolver();
    }
    @Bean
    public ContentNegotiatingViewResolver contentResolver() {
        ContentNegotiatingViewResolver cn = new ContentNegotiatingViewResolver();

        List<View> listOfViews = new ArrayList<View>();
        listOfViews.add(jsonView());
        listOfViews.add(xmlView());

        cn.setDefaultViews(listOfViews);

        return cn;
    }

    @Bean
    public MappingJackson2JsonView jsonView() {
        MappingJackson2JsonView mj= new MappingJackson2JsonView();
        mj.setPrettyPrint(true);
        return mj;
    }



    @Bean
    public MarshallingView xmlView() {
        Jaxb2Marshaller ja = new Jaxb2Marshaller();
        ja.setClassesToBeBound(Product.class);
        MarshallingView mv = new MarshallingView(ja);
        return mv;
    }
    @Bean
    public Validator validator() {
        LocalValidatorFactoryBean lv = new LocalValidatorFactoryBean();
        lv.setValidationMessageSource(messageSource());
        return lv;
    }
}

现在它并没有那么麻烦,因为当我手动设置应用程序上下文时 - 它的工作方式应该如此,但它的行为相当奇怪,而且我花了一些时间搞清楚这个问题,我觉得它不应该像这样工作。你能看看我的代码吗?提前致谢

0 个答案:

没有答案