@ -54,7 +54,15 @@ public class EventPublishingRepositoryProxyPostProcessor implements RepositoryPr
private final ApplicationEventPublisher publisher ;
private final ApplicationEventPublisher publisher ;
/ * *
* Creates a new { @link EventPublishingRepositoryProxyPostProcessor } for the given { @link ApplicationEventPublisher } .
*
* @param publisher must not be { @literal null } .
* /
public EventPublishingRepositoryProxyPostProcessor ( ApplicationEventPublisher publisher ) {
public EventPublishingRepositoryProxyPostProcessor ( ApplicationEventPublisher publisher ) {
Assert . notNull ( publisher , "Object must not be null" ) ;
this . publisher = publisher ;
this . publisher = publisher ;
}
}
@ -103,9 +111,9 @@ public class EventPublishingRepositoryProxyPostProcessor implements RepositoryPr
return result ;
return result ;
}
}
Object [ ] arguments = invocation . getArguments ( ) ;
Iterable < ? > arguments = asCollection ( invocation . getArguments ( ) [ 0 ] , invocation . getMethod ( ) ) ;
eventMethod . publishEventsFrom ( arguments [ 0 ] , publisher ) ;
eventMethod . publishEventsFrom ( arguments , publisher ) ;
return result ;
return result ;
}
}
@ -177,22 +185,18 @@ public class EventPublishingRepositoryProxyPostProcessor implements RepositoryPr
/ * *
/ * *
* Publishes all events in the given aggregate root using the given { @link ApplicationEventPublisher } .
* Publishes all events in the given aggregate root using the given { @link ApplicationEventPublisher } .
*
*
* @param object can be { @literal null } .
* @param aggregates can be { @literal null } .
* @param publisher must not be { @literal null } .
* @param publisher must not be { @literal null } .
* /
* /
public void publishEventsFrom ( @Nullable Object object , ApplicationEventPublisher publisher ) {
public void publishEventsFrom ( Iterable < ? > aggregates , ApplicationEventPublisher publisher ) {
if ( object = = null ) {
return ;
}
for ( Object aggregateRoot : asCollection ( object ) ) {
for ( Object aggregateRoot : aggregates ) {
if ( ! type . isInstance ( aggregateRoot ) ) {
if ( ! type . isInstance ( aggregateRoot ) ) {
continue ;
continue ;
}
}
for ( Object event : asCollection ( ReflectionUtils . invokeMethod ( publishingMethod , aggregateRoot ) ) ) {
for ( Object event : asCollection ( ReflectionUtils . invokeMethod ( publishingMethod , aggregateRoot ) , null ) ) {
publisher . publishEvent ( event ) ;
publisher . publishEvent ( event ) ;
}
}
@ -261,6 +265,8 @@ public class EventPublishingRepositoryProxyPostProcessor implements RepositoryPr
return method ;
return method ;
}
}
}
/ * *
/ * *
* Returns the given source object as collection , i . e . collections are returned as is , objects are turned into a
* Returns the given source object as collection , i . e . collections are returned as is , objects are turned into a
* one - element collection , { @literal null } will become an empty collection .
* one - element collection , { @literal null } will become an empty collection .
@ -269,12 +275,16 @@ public class EventPublishingRepositoryProxyPostProcessor implements RepositoryPr
* @return
* @return
* /
* /
@SuppressWarnings ( "unchecked" )
@SuppressWarnings ( "unchecked" )
private static Collection < Object > asCollection ( @Nullable Object source ) {
private static Iterable < Object > asCollection ( @Nullable Object source , @Nullable Method method ) {
if ( source = = null ) {
if ( source = = null ) {
return Collections . emptyList ( ) ;
return Collections . emptyList ( ) ;
}
}
if ( method ! = null & & method . getName ( ) . startsWith ( "saveAll" ) ) {
return ( Iterable < Object > ) source ;
}
if ( Collection . class . isInstance ( source ) ) {
if ( Collection . class . isInstance ( source ) ) {
return ( Collection < Object > ) source ;
return ( Collection < Object > ) source ;
}
}
@ -282,4 +292,3 @@ public class EventPublishingRepositoryProxyPostProcessor implements RepositoryPr
return Collections . singletonList ( source ) ;
return Collections . singletonList ( source ) ;
}
}
}
}
}