• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Extending Spring Boot to autoconfigure custom bean types

 
Ranch Hand
Posts: 90
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose that I have a custom bean type CustomBean with various swappable implementations. Is it possible to hook into Spring Boot's autoconfiguration system so that my app can autoconfigure a CustomBean?
 
author
Posts: 422
13
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure. At its core, auto-configuration isn't really that much magic. It's just a use-case for Spring 4.0's @Conditional. In short, all you need is a configuration class with @Bean-annotated method for whatever custom components you want, a @Conditional or two along with corresponding implementations of Condition and you're set. (Optionally...and perhaps preferably...you might take advantage of some of Spring Boot's pre-defined @ConditionalOnXXX annotations for common conditional scenarios.) Take a look at the Spring Boot auto-configuration source code at https://github.com/spring-projects/spring-boot to see examples of how it works under the covers.

That said, you only really need to provide auto-configuration for custom components if you plan on sharing those custom components with other teams/projects. If it's just for your app, then it's simple enough to write a @Configuration class and define the beans just like you would without Spring Boot...Java config classes will still be picked up (by way of component-scanning) and the beans therein created in the application context. (And yes, you can use XML if you really want to, but we don't like talking about that much. If you *REALLY* want to do XML config, see the @ImportResource annotation to see how to do it...but consider using Java config instead.)
 
reply
    Bookmark Topic Watch Topic
  • New Topic