daskai0815
daskai0815

Reputation: 177

Bean conflict in spring flow

I try to make a project with spring flow and jsf. I use gradle as build system with the following build.gradle:

    plugins {
    id 'java'
    id 'war'
    id 'org.springframework.boot' version '3.1.2'
    id 'io.spring.dependency-management' version '1.1.2'
}

group = 'test.pkg'
version = '0.0.1-SNAPSHOT'

java {
    sourceCompatibility = '17'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.joinfaces:mojarra-spring-boot-starter:5.1.2'
    compileOnly 'org.springframework.webflow:spring-webflow:3.0.0'
    implementation 'org.springframework.webflow:spring-webflow:3.0.0'
    implementation 'org.springframework.webflow:spring-faces:3.0.0'
    implementation 'jakarta.faces:jakarta.faces-api:4.0.1'

    runtimeOnly 'jakarta.faces:jakarta.faces-api:4.0.1'
    runtimeOnly 'org.springframework.webflow:spring-faces:3.0.0'
    runtimeOnly 'org.springframework.webflow:spring-webflow:3.0.0'
    runtimeOnly 'org.mariadb.jdbc:mariadb-java-client'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
    useJUnitPlatform()
}

I try to do all configuration in Java, so I have the following @Configuration class:

@Configuration
public class WebFlowConfig extends AbstractFacesFlowConfiguration {

    @Bean
    public FlowDefinitionRegistry flowDefinitionRegistry(FlowBuilderServices flowBuilderServices) {
        return getFlowDefinitionRegistryBuilder()
                .setBasePath("/WEB-INF")
                .addFlowLocation("/flows/**/*.xml")
                .setFlowBuilderServices(flowBuilderServices) // 11.5 view resolution
                .build();
    }

    @Bean
    public FlowExecutor flowExecutor(FlowDefinitionRegistry flowDefinitionRegistry) {
        return getFlowExecutorBuilder(flowDefinitionRegistry)
                .addFlowExecutionListener(new FlowFacesContextLifecycleListener())  // 13.3
                .build();
    }

    @Bean
    public FlowBuilderServices flowBuilderServices(MvcViewFactoryCreator mvcViewFactoryCreator) {
        return getFlowBuilderServicesBuilder()
                .setDevelopmentMode(true)
                .setViewFactoryCreator(mvcViewFactoryCreator)
                .build();
    }

    @Bean
    public MvcViewFactoryCreator mvcViewFactoryCreator() {
        MvcViewFactoryCreator mvcViewFactoryCreator = new MvcViewFactoryCreator();
        return mvcViewFactoryCreator;
    }

    //13.2
    @Bean
    public ServletRegistrationBean jsfServletBean() {
        ServletRegistrationBean<jakarta.faces.webapp.FacesServlet> servletRegistrationBean = new ServletRegistrationBean(new FacesServlet(), "*.xhtml");
        servletRegistrationBean.setLoadOnStartup(1);
        return servletRegistrationBean;
    }

    //auch 13.2
    @Bean
    public ServletContextInitializer initializer() {
        return new ServletContextInitializer() {
            @Override
            public void onStartup(ServletContext servletContext) throws ServletException {
                servletContext.setInitParameter("javax.faces.DEFAULT_SUFFIX", ".xhtml");
            }
        };
    }

    //13.3
    public JsfFlowHandlerAdapter jsfFlowHandlerAdapter(FlowExecutor flowExecutor) {
        JsfFlowHandlerAdapter jsfFlowHandlerAdapter = new JsfFlowHandlerAdapter();
        jsfFlowHandlerAdapter.setFlowExecutor(flowExecutor);
        return jsfFlowHandlerAdapter;
    }

    @Bean
    public ServletRegistrationBean webFlowServletBean() {
        ServletRegistrationBean<DispatcherServlet> bean = new ServletRegistrationBean(new DispatcherServlet(), "/flows/*");
        bean.addInitParameter("contextConfigLocation", "/WEB-INF/web-application-config.xml");
        bean.setLoadOnStartup(1);
        bean.setOrder(1);
        return bean;
    }

    @Bean
    public FlowHandlerMapping flowHandlerMapping(FlowDefinitionRegistry flowDefinitionRegistry) {
        FlowHandlerMapping flowHandlerMapping = new FlowHandlerMapping();
        flowHandlerMapping.setFlowRegistry(flowDefinitionRegistry);
        flowHandlerMapping.setOrder(0);
        return flowHandlerMapping;
    }
}

but when I try to run this project I get the following error:

The bean 'httpRequestHandlerAdapter', defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [test/pkg/testflow/config/WebFlowConfig.class] and overriding is disabled.

I don't want to enable overriding since I want my bean to be used. This is why I have no Idea how to solve this conflict. Is there a way to do web flow projects with jsf and all config in Java ?

Best regards Kai

Upvotes: 0

Views: 66

Answers (0)

Related Questions