-
Notifications
You must be signed in to change notification settings - Fork 38.5k
@Bean on Java 8 default methods in interfaces [SPR-10919] #15547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thomas Darimont commented An extension to this would be the support of pure interface based configurations - but I think this is another issue by itself, since it requires a proxy (or an instance of a dynamically generated class) to be created in order to be able invoke the default methods. Here is a small example in the context of the scenario mentioned above: @Configuration
interface ApplicationConfig extends ServiceConfig, PersistenceConfig {
@Bean
default Application application() {
return new Application(service1(), dataStore());
}
} |
Thomas Darimont commented Added PR: #368 |
Oliver Drotbohm commented As per the discussion with Juergen Hoeller today, this ticket can be expanded to the general support of abstract JavaConfig types. This is achieved by actually defining semantics of (currently unsupported) abstract The suggested semantics are that an abstract method is interpreted like a manual (XML) bean definition with only the @Configuration
interface ApplicationConfig extends ServiceConfig, PersistenceConfig {
@Bean default Application application() {
return new Application(service1(), dataStore());
}
@Bean MyApplicationComponent beanName();
} The container would create a |
Juergen Hoeller commented I'm pushing this back to the 4.x backlog since we're on a rather tight schedule towards 4.1 now (with a GA target in July). So let's rather consider this for 4.2 (which is what the 4.x backlog means at the moment). I did play a bit with the code there... There are a couple of challenges: introspecting Juergen |
Thomas Darimont commented Btw. another use case for Instead of creating a custom A test context listener (defined via SpringJUnit4ClassRunner) could then recognize the defined interface FooService{} interface CommonMocks{
default @Bean FooService fooService() { return Mockito.mock(FooService.class); }
} ...
class SomeTest implements CommonMocks{
@Autowired FooService fooService;
@Test
public void someTest(){
assertThat(fooService.businessMethod(), ....)
}
} |
Oliver Drotbohm commented Keep the examples coming, Thomas! I like them quite a lot and they'd just make the story more convincing. |
Thomas Darimont commented Great thanks :) As an alternative to the above - one could also create: SAM Type interfaces for Mock components: public interface CommonMocks {
public static interface WithMockDataService {
@Bean
default DataService dataService() {
return mock(DataService.class);
}
}
public static interface WithMockNotificationService {
@Bean
default NotificationService notificationService() {
return mock(NotificationService.class);
}
}
} import static ...CommonMocks.*;
...
class SomeTest implements WithMockDataService{ //Just import the mocking variant you need
@Autowired DataService dataService;
@Test
public void someTest(){
assertThat(dataService.businessMethod(), ....)
}
} |
Juergen Hoeller commented Let's reduce this ticket to The good news: With this reduced scope, it's basically done and will make it into master by the end of this week! Note that we're only supporting default methods on implemented interfaces at this point. Configuration classes still need to be concrete; they may implement interfaces and inherit default Configuration interfaces purely based on default methods would be quite a significant step beyond that, and raise some serious questions about whether that constitutes a misuse of default methods. This is not planned for 4.2, and might not ever happen. Juergen |
Juergen Hoeller commented The Juergen |
Thomas Darimont opened SPR-10919 and commented
It would be great if the JavaConfig configuration style would support the definition of
@Bean
Bean-Definitions via Java 8's default methods.To get this working it is necessary, that default methods are also considered while scanning a type for
@Bean
Bean-Definitions.Here is a small example on how this would look like:
Affects: 4.0 M3
Issue Links:
@Configuration
interface with Java 8 default methods (as a standalone artifact)@Import
Referenced from: commits 1924629
4 votes, 11 watchers
The text was updated successfully, but these errors were encountered: