Skip to content

@Configuration interface with Java 8 default methods (as a standalone artifact) [SPR-14220] #18794

Closed
@spring-projects-issues

Description

@spring-projects-issues

joerg.hohwiller@googlemail.com opened SPR-14220 and commented

#15547 brought an awesome improvement to support @Bean in Java8 default methods of interfaces. In the end I currently still have to implement an @Configuration class that implements the interfaces. Assuming that all methods of the interface(s) are default method implementing the @Configuration class is just unnecessary code if it just implements one interface. Why not have support directly for

@SpringApplicationConfiguration(classes = MyConfigInterface.class)

This was already slightly discussed in #15547 but considered out of scope.

The major advantage of (miss)using interfaces for configs is that you can:

  • use inheritance instead of @Import what is more natural and even more safe as the compiler can detect conflicts.
  • as an end-user of a flexible designed library just define your config by extending according @Configuration interfaces from the library but still selectively override @Bean methods where ever you want to customize. When using @Import this is not easily possible.
  • as an author of a library you can provide your @Configuration objects in such a flexible way as interfaces but without the burden to have a corresponding class for each interface that does noting but implement the interface. If you have a very modular library you might have many @Configuration objects (see e.g. add spring @Configuration classes m-m-m/util#178).

To summarize I like to propose that with Java8 the interface is the new top-level language element. It would be great to see this even more supported by spring. I already use default methods to implement QueryDSL queries in default methods with pure interfaces and no single repository implementation class. This already works today with current spring releases (Thanks guys for your great work). See also m-m-m/util#163 for more advanced usage of Java8 interfaces.


Affects: 4.2.5

Issue Links:

0 votes, 5 watchers

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: declinedA suggestion or change that we don't feel we should currently applytype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions