This repository has been archived by the owner on Jul 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from ralf-ueberfuhr-ars/features/interceptors
Introduce interceptors
- Loading branch information
Showing
6 changed files
with
89 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/java/de/sample/schulung/spring/blog/domain/interceptors/PublishEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package de.sample.schulung.spring.blog.domain.interceptors; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* Annotate a method to get an event fired after method execution. | ||
*/ | ||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Inherited | ||
@Documented | ||
public @interface PublishEvent { | ||
|
||
/** | ||
* The event class. This class needs a constructor with the same parameters as the method. | ||
* | ||
* @return the event class | ||
*/ | ||
Class<?> value(); | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
...main/java/de/sample/schulung/spring/blog/domain/interceptors/PublishEventInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package de.sample.schulung.spring.blog.domain.interceptors; | ||
|
||
import org.aopalliance.intercept.MethodInterceptor; | ||
import org.springframework.aop.Pointcut; | ||
import org.springframework.aop.framework.autoproxy.AbstractBeanFactoryAwareAdvisingPostProcessor; | ||
import org.springframework.aop.support.DefaultPointcutAdvisor; | ||
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut; | ||
import org.springframework.beans.factory.InitializingBean; | ||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.core.annotation.AnnotationUtils; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class PublishEventInterceptor | ||
extends AbstractBeanFactoryAwareAdvisingPostProcessor | ||
implements InitializingBean { | ||
|
||
private final MethodInterceptor publishEventAdvice; | ||
|
||
public PublishEventInterceptor(final ApplicationEventPublisher eventPublisher) { | ||
this.publishEventAdvice = invocation -> { | ||
// create event object | ||
Object event = null; | ||
// find @PublishEvent annotation on invoked method | ||
final var annotation = AnnotationUtils.findAnnotation(invocation.getMethod(), PublishEvent.class); | ||
if(null != annotation) { | ||
event = annotation.value() // event type | ||
// event type must have a constructor with the same type as the invoked method | ||
// service.create(blogPost) -> new BlogPostCreatedEvent(blogPost) | ||
.getConstructor(invocation.getMethod().getParameterTypes()) | ||
.newInstance(invocation.getArguments()); | ||
} | ||
// if something is wrong until here, we do not invoke the service's create-method | ||
// now, we invoke the service | ||
final var result = invocation.proceed(); | ||
// if an exception occured, the event is not fired | ||
// now, we fire the event | ||
if(null != event) { | ||
eventPublisher.publishEvent(event); | ||
} | ||
// and we need to return the service's result to the invoker (the controller) | ||
return result; | ||
}; | ||
} | ||
|
||
@Override | ||
public void afterPropertiesSet() { | ||
Pointcut pointcut = new AnnotationMatchingPointcut(null, PublishEvent.class, true); | ||
this.advisor = new DefaultPointcutAdvisor(pointcut, publishEventAdvice); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters