Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-13779

Programmatic bean registration within configuration classes

    XMLWordPrintable

    Details

    • Type: New Feature
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: 5.x Backlog
    • Component/s: Core:DI
    • Labels:
      None
    • Last commented by a User:
      true

      Description

      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.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              juergen.hoeller Juergen Hoeller
              Reporter:
              rwinch Rob Winch
              Last updater:
              Spring Issuemaster
              Votes:
              3 Vote for this issue
              Watchers:
              17 Start watching this issue

                Dates

                Created:
                Updated:
                Days since last comment:
                1 year, 37 weeks, 2 days ago