Skip to content

Programmatic bean registration with configuration classes #18353

Closed
@spring-projects-issues

Description

@spring-projects-issues

Rob Winch opened SPR-13779 and commented

It would be nice to be able to allow Java Configuration to register multiple types of Beans. For example, right now the Spring Security exposes a Java DSL like this:

public void configure(HttpSecurity http) {
    http
        .formLogin()
}

This single invocation (made by the developer configuring Spring Security) should ideally create numerous Beans (i.e. UsernamePasswordAuthenticationFilter, AuthenticationEntryPoint, etc) and expose them to the Spring ApplicationContext.

The key takeaway is that a developer should be able to interact with a DSL where a single invocation creates multiple Beans.

This is something Juergen Hoeller and I spoke about briefly at SpringOne that I would like to get on the roadmap (hopefully for Spring 5).

Updated

To elaborate on my comment below, I think it would be nice if we could do something like this:

class MyDsl {
   private boolean addABean;
   private boolean addBBean;
   // getters /setters
}

class MyDslXmlParser {
   MyDsl parse(Document d) {
      return createDls(d);
   }
}

class MyDslParser {
    public void registerBeans(MyDsl dsl, BeanFactory) {
        if(dsl.isAddABean()) {
            bf.registerBean(new A());
        }
        if(dsl.isAddBBean()) {
            bf.registerBean(new B());
        }
    }
}

I Java Config Users could consume this with:

class JavaConfig {
    @Bean
    public MyDsl myDsl() {
        MyDsl myDsl = new MyDsl();
        myDsl.setAddABean(true);
        return myDsl;
    }
}

and MyDslParser.registerBeans would automatically be invoked with the proper arguments.

In XML Config users could consume this with:

<mydsl:mydsl aBean="true" />

and MyDslParser.registerBeans would automatically be invoked with the proper arguments.

This would allow the framework to easily support multiple ways of configuring the Beans.


Issue Links:

2 votes, 16 watchers

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions