Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added @AnnotationDriven annotation for Spring Java Config
This annotation removes the need to "manually" configure the annotation postprocessor beans when using Java Config. Issue #AXON-240 Fixed
- Loading branch information
Showing
7 changed files
with
430 additions
and
35 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
68 changes: 68 additions & 0 deletions
68
core/src/main/java/org/axonframework/contextsupport/spring/AnnotationDriven.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,68 @@ | ||
/* | ||
* Copyright (c) 2010-2014. Axon Framework | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.axonframework.contextsupport.spring; | ||
|
||
import org.springframework.context.annotation.Import; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Annotation for {@link org.springframework.context.annotation.Configuration @Configuration} that will automatically | ||
* subscribe {@link org.axonframework.commandhandling.annotation.CommandHandler @CommandHandler} and {@link | ||
* org.axonframework.eventhandling.annotation.EventHandler @EventHandler} annotated beans with the CommandBus and | ||
* EventBus, respectively. | ||
* <p/> | ||
* If a context contains multiple EventBus or CommandBus implementations, you must indicate the isntance to use as | ||
* a property on this annotation. | ||
* | ||
* @author Allard Buijze | ||
* @since 2.3 | ||
*/ | ||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@Import(AnnotationDrivenConfiguration.class) | ||
public @interface AnnotationDriven { | ||
|
||
/** | ||
* The bean name of the Event Bus to register {@link | ||
* org.axonframework.eventhandling.annotation.EventHandler @EventHandler} annotated beans with. | ||
*/ | ||
String eventBus() default ""; | ||
|
||
/** | ||
* The bean name of the Command Bus to register {@link | ||
* org.axonframework.commandhandling.annotation.CommandHandler @CommandHandler} annotated beans with. | ||
*/ | ||
String commandBus() default ""; | ||
|
||
/** | ||
* Whether to unsubscribe beans on shutdown. Default to <code>false</code>. Setting this to <code>true</code> will | ||
* explicitly unsubscribe beans from the Event- and CommandBus when shutting down the application context. | ||
*/ | ||
boolean unsubscribeOnShutdown() default false; | ||
|
||
/** | ||
* The phase in the application context lifecycle in which to subscribe (and possibly unsubscribe) the handlers | ||
* with the CommandBus and EventBus. | ||
*/ | ||
int phase() default 0; | ||
} |
67 changes: 67 additions & 0 deletions
67
.../src/main/java/org/axonframework/contextsupport/spring/AnnotationDrivenConfiguration.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,67 @@ | ||
/* | ||
* Copyright (c) 2010-2014. Axon Framework | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.axonframework.contextsupport.spring; | ||
|
||
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; | ||
import org.springframework.core.type.AnnotationMetadata; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Spring @Configuration related class that adds Axon Annotation PostProcessors to the BeanDefinitionRegistry. | ||
* | ||
* @author Allard Buijze | ||
* @see org.axonframework.contextsupport.spring.AnnotationDriven | ||
* @since 2.3 | ||
*/ | ||
public class AnnotationDrivenConfiguration implements ImportBeanDefinitionRegistrar { | ||
|
||
private static final String ANNOTATION_TYPE = AnnotationDriven.class.getName(); | ||
|
||
@Override | ||
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { | ||
final AnnotationConfigurationBeanDefinitionParser parser = new AnnotationConfigurationBeanDefinitionParser(); | ||
Map<String, Object> attributes = importingClassMetadata.getAnnotationAttributes(ANNOTATION_TYPE); | ||
parser.registerAnnotationCommandHandlerBeanPostProcessor(getCommandBus(attributes), getPhase(attributes), | ||
getUnsubscribeOnShutdown(attributes), | ||
registry); | ||
parser.registerAnnotationEventListenerBeanPostProcessor(getEventBus(attributes), getPhase(attributes), | ||
getUnsubscribeOnShutdown(attributes), | ||
registry); | ||
} | ||
|
||
private String getEventBus(Map<String, Object> attributes) { | ||
final Object eventBus = attributes.get("eventBus"); | ||
return eventBus == null ? null : eventBus.toString(); | ||
} | ||
|
||
private String getCommandBus(Map<String, Object> attributes) { | ||
final Object commandBus = attributes.get("commandBus"); | ||
return commandBus == null ? null : commandBus.toString(); | ||
} | ||
|
||
private String getUnsubscribeOnShutdown(Map<String, Object> attributes) { | ||
final Boolean unsubscribeOnShutdown = (Boolean) attributes.get("unsubscribeOnShutdown"); | ||
return unsubscribeOnShutdown == null ? null : Boolean.toString(unsubscribeOnShutdown); | ||
} | ||
|
||
private String getPhase(Map<String, Object> attributes) { | ||
final Integer phase = (Integer) attributes.get("phase"); | ||
return phase == null ? null : Integer.toString(phase); | ||
} | ||
} |
Oops, something went wrong.